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 trialcathy mitchell
8,249 Pointschallenge 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
26,676 PointsHi 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.
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;
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!
Jason Anders
Treehouse Moderator 145,860 PointsHey 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. :)
module.exports.root = root;
module.exports.contact = contact;
module.exports.about = about;
Keep Coding! :)
cathy mitchell
8,249 PointsJason,
Here is the code I get when I put the above in
Bummer! Routes is not defined
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;
cathy mitchell
8,249 Pointscathy mitchell
8,249 Pointshere 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
26,676 PointsChris Shaw
26,676 PointsI 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?
cathy mitchell
8,249 Pointscathy mitchell
8,249 PointsChris,
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.