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
Roy G
7,955 Pointsif(request.url === "/"): Cannot read 'url' property of undefined
I checked and double checked and my code looks ok but I'm still getting this error:
/home/treehouse/workspace/router.js:2
if(request.url === "/"){
^
TypeError: Cannot read property 'url' of undefined
at home (/home/treehouse/workspace/router.js:2:13)
at Object.<anonymous> (/home/treehouse/workspace/router.js:20:23)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/home/treehouse/workspace/app.js:1:76)
at Module._compile (module.js:409:26)
My router.js file is:
function home(request, response){
if(request.url === "/"){
response.write("Header \n");
response.write("Search \n");
response.write("footer \n");
}
}
function user(request, response){
var username = request.url.replace("/", "");
if(username.length > 0)
{
response.write("Header \n");
response.write(username + "\n");
response.write("Foother");
}
}
app.js:
var router = require("./router.js");
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type' : 'text/plain'});
router.home(request, response);
router.user(request, response);
}).listen(3000);
console.log('Server running at http://<workspace-url>/');
Any ideas?
2 Answers
srikarvedantam
8,369 PointsPerhaps you are missing 'http' module requirement:
var http = require('http');
srikarvedantam
8,369 Points"module.exports" are missing. I modified your "router.js" as below and it's working fine:
[router.js]
function home(request, response){
if(request.url === "/"){
response.write("Header \n");
response.write("Search \n");
response.write("footer \n");
response.end(0);
}
}
function user(request, response) {
var username = request.url.replace("/", "");
if (username.length > 0) {
response.write("Header \n");
response.write(username + "\n");
response.write("Foother");
response.end(0);
}
}
module.exports.home = home;
module.exports.user = user;
Roy G
7,955 PointsRoy G
7,955 PointsI have that in the 3rd line of the app.js