Browse Source

Move nunjucks middleware into own module

pm2
Luke Childs 8 years ago
parent
commit
4d9b0ffaeb
  1. 21
      index.js
  2. 7
      lib/nunjucks-middleware.js

21
index.js

@ -1,20 +1,15 @@
const nunjucks = require('nunjucks');
const express = require('express');
const nunjucksFilters = require('./lib/nunjucks-filters');
const controllers = require('./controllers');
const app = express();
const port = process.env.port || 3000;
const nunjucks = require('nunjucks');
const express = require('express');
const nunjucksFilters = require('./lib/nunjucks-filters');
const nunjucksMiddleware = require('./lib/nunjucks-middleware');
const controllers = require('./controllers');
const app = express();
const port = process.env.port || 3000;
app.set('nunjucksEnv', nunjucks.configure('views', { express: app }));
nunjucksFilters(app);
app.use((req, res, next) => {
req.app.settings.nunjucksEnv
.addGlobal('req', req)
.addGlobal('res', res);
next();
});
app.use(nunjucksMiddleware);
app.get('/', controllers.listing);
app.get('/node/:id', controllers.node);

7
lib/nunjucks-middleware.js

@ -0,0 +1,7 @@
module.exports = (req, res, next) => {
req.app.settings.nunjucksEnv
.addGlobal('req', req)
.addGlobal('res', res);
next();
}
Loading…
Cancel
Save