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 trialJames White
847 Pointsrenderer.view only displays header and search but not footer
I am doing the "Build a Simple Dynamic Site with Node.js" course and I'm on this video
I have a problem with renderer.view
it's not displaying the footer no matter what I do.
var Profile = require("./profile");
var render = require('./render');
function home(request, response) {
if(request.url === '/') {
response.writeHead(200, {'Content-Type': 'text/plain'});
render.view('header', {}, response);
render.view('search', {}, response);
render.view('footer', {}, response);
}
}
The code above just doesn't load the server at all, the page keeps loading and eventually I get Page isn't loading
Failed to load resource: net::ERR_CONNECTION_RESET
if I replace the footer part with response.end('Footer');
the page loads but it displays the word Footer
instead of loading the content of footer.html
If I test it with only the header part, the page doesn't load, if I test it with header & search or header & footer, the page doesn't load either.
I have no idea what's going on.
This is render.js
var fs = require('fs');
function view(templateName, values, response) {
var content = fs.readFileSync('./views/' + templateName + '.html');
response.write(content);
}
module.exports.view = view;
2 Answers
Seth Kroger
56,413 Pointsresponse.end()
needs to be called at some point to let node and the browser know you've reached the end of the page and to close off the response. render.view()
only uses response.write()
, so you'll need to add an empty response.end()
to finish things off.
James White
847 PointsThank you Seth, that fixed it.
But there's a silly story behind this. I decided to pause the video after I received this error and I've been researching it for the past 2 hours without any answers.
After you gave me the solution, I resumed the video only to see that Andrew Chalkley gives the answer to the problem a few seconds later.
I can be dense sometimes.