Sunday, 6 March 2016

Combine template engine with express.static

Hello everyoneIs there a way to serve static files like this:app.use(express.static(__dirname + '/views/pages')); while using a templating engine like ejs so I can make use of partials? The example I used demonstrates partials like this:app.set('view engine', 'ejs'); // use res.render to load up an ejs view file // index page app.get('/', function(req, res) { res.render('pages/index'); }); // about page app.get('/about', function(req, res) { res.render('pages/about'); }); This way of working would force me to add 3 new lines for every new file I would want to add which doesn't seem ideal.So far, I tried this as a solution:var files = [ 'index', 'about' ]; for(var i = 0; i< 2; i++){ app.get(path.join('/', files[i]), function(req, res) { res.render(path.join('pages',files[i])); }); }; However, this returnsCannot GET / The folder structure is exactly like in the earlier linked example, like this:/views /partials /pages index.ejs about.ejs package.json server.js Does anyone have a more elegant way to present a lot of static content while using a templating engine like ejs?

Submitted March 06, 2016 at 03:46PM by XDfaceme

No comments:

Post a Comment