Create a CRUD App with Node and MongoDB


ejs and express-ejs-layouts will be handling templating duties for us in this application. We'll need to make sure our Node app has some configuration setup:

// tell express where to look for static assets
app.use(express.static(__dirname + '/public'));

// set ejs as our templating engine
app.set('view engine', 'ejs');

After configuration, we can create a file called views/layout.ejs to use as the overall layout. It will know when to pull in page info when we use the <%- body %> tag.

Also, if you wanted to define another layout (let's say a views/sidebar-layout.ejs), you could pass that into the res.render() like so:

res.render('pages/contact', { layout: 'sidebar-layout' });

Like this article? Follow @chrisoncode on Twitter