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 trialBrian Patterson
19,588 PointsReferenceError: response is not defined
When go to the server I am getting the following error:
Server running at http://<workspace-url>/
/Users/briankaty1/Dropbox/JavaScript/simple_node_site/renderer.js:14
response.write(fileContents);
^
ReferenceError: response is not defined
at Object.view (/Users/briankaty1/Dropbox/JavaScript/simple_node_site/renderer.js:14:3)
at Object.home (/Users/briankaty1/Dropbox/JavaScript/simple_node_site/router.js:11:12)
at Server.<anonymous> (/Users/briankaty1/Dropbox/JavaScript/simple_node_site/app.js:36:10)
at emitTwo (events.js:106:13)
at Server.emit (events.js:191:7)
at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:546:12)
at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)
Below is the code I have writtent in the renderer.js file.
var fs = require("fs");
//Function that handles the reading of files and merge in value.
// read from file and get a string
//merge values in to a string.
function view(templateName, values, reponse) {
//Read from the template file
var fileContents = fs.readFileSync('./views/' + templateName + '.html');
//Insert values in to the content
//write out the contents to the response.
response.write(fileContents);
}
module.exports.view = view;
This is what I have written in my router file.
var Profile = require("./profile.js");
var renderer = require("./renderer.js");
//## Handle HTTP route GET / and POST / i.e. Home
function home(request, response) {
// - if url == "/" && GET
if(request.url === "/") {
//show search
response.writeHead(200, {'Content-Type': 'text/plain'});
renderer.view("header", {}, response);
response.write("Search\n");
response.end('Footer\n');
// - if url == "/" && POST
// redirect to /:username
}
}
function user(request, response) {
var username = request.url.replace("/", "");
if(username.length > 0) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write("Header\n");
//Get json from Treehouse
var studentProfile = new Profile(username);
//on "end"
studentProfile.on("end", function(profileJSON){
//show profile
//Store the values which we need.
var values = {
avatarUrl: profileJSON.gravatar_url,
username: profileJSON.profile_name,
badges: profileJSON.badges.length,
javascriptPoints: profileJSON.points.JavaScript
}
//Simple response
response.write(values.username + " has " + values.badges + " badges\n");
response.end('Footer\n');
});
/**
* If a parsing, network or HTTP error occurs an
* error object is passed in to the handler or callback
**/
studentProfile.on("error", function(error){
//show error
response.write(error.message + "\n");
response.end('Footer\n');
});
}
}
//## Handle HTTP route GET /:username i.e. /chalkers
module.exports.home = home;
module.exports.user = user;
Any help would be appreciated.
1 Answer
Joel Bardsley
31,249 PointsHi Brian,
response is not defined because you have an unfortunate typo in the parameters of your view function:
function view(templateName, values, reponse) {
Just make that small correction and you should be all set.
Brian Patterson
19,588 PointsBrian Patterson
19,588 PointsThanks Joel.