Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.