Video Course

Create a CRUD App with Node and MongoDB: Lesson 3 of 17


Up Next

All Events

Autoplaying in 7 seconds!


Create a Free Account to Watch

Sign Up Now

Login to your account

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' });

Chris Sevilleja

156 posts

Co-founder of Slapping the keyboard until something good happens.