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

pastry assassin
seal-mask
.a{fill-rule:evenodd;}techdegree
pastry assassin
Full Stack JavaScript Techdegree Student 11,693 Points

Routes Node.JS code challenge

I'm not entirely clear on what the code challenge is asking me to do. It's saying that I need to create an about function to print out "About\n" inside of the routes.js file and add it to the abouts route. Can someone help me with this?

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");
    }
}

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

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

1 Answer

Steven Parker
Steven Parker
218,708 Points

Take a look at the other two functions already there, particularly the one that implements "contact". What the challenge wants you to do is make a function just like that, but for "about" instead of "contact". Use the contact function as a template and it you should not have much trouble.

Don't forget to add it to the exports down at the bottom, also.