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 Build a Simple Dynamic Site with Node.js Handling Routes in Node.js Home Route

John Coolidge
John Coolidge
12,614 Points

Workspaces won't respond to preview

I'm working on Andrew's course and so far as I can tell I've got everything he's shown in my workspaces. When I try to run the app it logs that the server is running (like the code shows) but when I try to preview the workspaces I get a page that says "Workspace Unavailable".

The console then says that the error is a ReferenceError: response is not defined.

at homeRoute (/home/treehouse/workspace/app.js:16:5)
at Server.<anonymous> (/home/treehouse/workspace/app.js:8:3)
at Server.emit (events.js:110:17)
at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:4 91:12)
at HTTPParser.parserOnHeadersComplete (_http_common.js:111:23)
at Socket.socketOnData (_http_server.js:343:22)
at Socket.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
at Socket.Readable.push (_stream_readable.js:126:10)
at TCP.onread (net.js:538:20)

I've killed the program and reran it and the same happens.

What response is not defined?

//1. Create a webserver

var http = require('http');
http.createServer(function (request, response) {
  homeRoute(request, response);
}).listen(3000);
console.log('Server running at http://<workspace-url/');

//2. Handle HTTP route GET / and POST / i.e. Home
function homeRoute(request, reponse) {
   //if url == "/" && GET
    //show search
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.write("Header\n");
    response.write("Search\n");
    response.end("Footer\n");
  //if url == "/" && POST
    //redirect to /:username
}
John Coolidge
John Coolidge
12,614 Points

Nevermind! I found my error. I wrote reponse and not response in the homeRoute function.