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 Handling Routes Review

challenge task 1 of 2. Node.js,

We've been tasked to continue with this prototype site. Have a look in the index.js and routes.js file and familiarize yourself. In the routes.js, just like the other two functions create an about function for a /about route. Have it print out "About\n" to the response. Remember to make this accessible by other files.

What have I missed?????

here is my code

function root(request, response) {
    if(request.url == "/") {
        response.writeHead(200, {'Content-type': "text/plain"});
        response.end("Home\n");
    }
}

function contact(request, response) {
    if(request.url == "/contact") {
        response.writeHead(200, {'Content-type': "text/plain"});
        response.end("Contact\n");
    }
}
function about(request, response) {
  if(request.url == "/about")  {
    response.writeHead(200, {"Content-type" : "text/plain"});
    response.end("About\n");
  }
}

module.exports.root = root;
module.exports.contact = contact;
var http = require("http");
var routes = require("./routes.js");

http.createServer(function(request, response){
    routes.root(request, response);
    routes.contact(request, response);
}).listen(3000);

3 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi cathy mitchell,

You're almost there; you simply need to export your about function in routes.js and call it from within the createServer callback function.

routes.js
function root(request, response) {
    if(request.url == "/") {
        response.writeHead(200, {'Content-type': "text/plain"});
        response.end("Home\n");
    }
}

function contact(request, response) {
    if(request.url == "/contact") {
        response.writeHead(200, {'Content-type': "text/plain"});
        response.end("Contact\n");
    }
}

function about(request, response) {
  if(request.url == "/about")  {
    response.writeHead(200, {"Content-type" : "text/plain"});
    response.end("About\n");
  }
}

module.exports.root = root;
module.exports.contact = contact;
module.exports.about = about;
index.js
var http = require("http");
var routes = require("./routes.js");

http.createServer(function(request, response){
    routes.root(request, response);
    routes.contact(request, response);
    routes.about(request, response);
}).listen(3000);

Happy coding!

here is the error message I am getting :

Bummer! Routes is not defined.

I copied what you had there and it didn't work as you can see..

Chris Shaw
Chris Shaw
26,676 Points

I just tried the same code above again and it works without any issues, are you sure you have all three functions defined and exported? Could you take a screenshot of what you have currently?

Chris,

Thank you for all your help. It finally went through. I had to log out and go back and reinsert it for the 4th time.

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hey Cathy,

You may kick yourself when I tell you... You code is all correct. You just forgot to export the module. At the bottom of the other two, you missed adding the "about" export. :)

routes.js
module.exports.root = root;
module.exports.contact = contact;
module.exports.about = about;

Keep Coding! :) :dizzy:

Jason,

Here is the code I get when I put the above in

Bummer! Routes is not defined

Abinet Kenore
Abinet Kenore
10,082 Points

/HERE YOU/WE GO/ function root(request, response) { if(request.url == "/") { response.writeHead(200, {'Content-type': "text/plain"}); response.end("Home\n"); } }

function contact(request, response) { if(request.url == "/contact") { response.writeHead(200, {'Content-type': "text/plain"}); response.end("Contact\n"); } }

function about(request, response) { if(request.url == "/about") { response.writeHead(200, {"Content-type" : "text/plain"}); response.end("About\n"); } } var http = require("http"); var routes = require("./routes.js");

http.createServer(function(request, response){ routes.root(request, response); routes.contact(request, response); routes.about(request, response); }).listen(3000);

module.exports.root = root; module.exports.contact = contact; module.exports.about = about;