Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Express Basics (2015) Using Templates with Express Review the res.render method

Julia J
Julia J
6,251 Points

Injecting posts object into template

When I added the code: res.render('posts', {posts: 'posts'}); like how they did it in the video, it wouldn't work. I put it inside this section of code: app.get('/', function(req, res){ res.render('index') res.render('main') });

However, when I did that, it said that task 1 was no longer passing. (adding the res.render('main') was task one)

Am I doing the right method, and just putting it in the wrong spot, or should I use a different line of code altogether?

app.js
'use strict';

var express = require('express'),
  posts = require('./mock/posts.json');

var app = express();

app.set('view engine', 'jade');
app.set('views', __dirname + '/templates')

app.get('/', function(req, res){
  res.render('index')
  res.render('main')
});

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

1 Answer

Arturo Alviar
Arturo Alviar
15,739 Points

Hi Kevin,
The second question of this challenge was worded a bit weird so you may have misunderstood it. You are doing the right thing but passing in the object to the wrong template file. In this case we want to do this:

res.render('main', {posts: posts})