JavaScript Express Basics Using Templates with Express Review Response.render

Kris Thayer
PLUS
Kris Thayer
Courses Plus Student 1,590 Points

If the following variable was set in the Express app, how do you pass the posts object to your template, naming it "post

const posts = require('./mock/posts.json');

app.js
const express = require('express');
const posts = require('./mock/posts.json');

const app = express();

app.set('view engine', 'pug');
app.set('views', __dirname + '/templates')
posts.set ('/posts', (req, res) => {
  res.render('posts');
});
app.get('/', (req, res) => {
  res.render('main');
});

app.listen(3000, () => {
  console.log("The frontend server is running on port 3000!");
});

1 Answer

Byron Injeeli
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Byron Injeeli
Full Stack JavaScript Techdegree Graduate 25,344 Points

Hi Kris, If you want to pass the posts variable to your template, you will need to do this:

app.get('/posts', (req, res) => {
  res.render('main', {posts: posts});
});