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 User Route

Caleb McGowan
Caleb McGowan
5,289 Points

TypeError: Cannot read property 'replace' of undefined

Whenever I attempt to view the preview, the console produces the following error. (the ^ points at .replace).

var username = res.url.replace("/", "");                                                                                                                                                                                                     
                         ^                                                                                                                                                                                                                     

TypeError: Cannot read property 'replace' of undefined                                                                                                                                                                                         
    at Object.user (/home/treehouse/workspace/router.js:13:26)                                                                                                                                                                                 
    at Server.http.createServer (/home/treehouse/workspace/app.js:8:10)                                                                                                                                                                        
    at emitTwo (events.js:126:13)                                                                                                                                                                                                              
    at Server.emit (events.js:214:7)                                                                                                                                                                                                           
    at parserOnIncoming (_http_server.js:660:12)                                                                                                                                                                                               
    at HTTPParser.parserOnHeadersComplete (_http_common.js:119:17)

My current code is below: app.js

var router = require('./router.js');


//create web server
const http = require('http');
// const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
  router.home(req, res);
  router.user(req, res);
});
server.listen(port, () => {
  console.log(`Server running at http://treehouse-server:${port}/`);
});

router.js

var Profile = require("./profile.js");

//Handle HTTP route GET / and POST / i.e. Home
function home(req, res){
  if(req.url === '/'){
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.write('Header\n');
    res.write('Search\n');
    res.end('Footer\n');
  }
}

//Handle HTTP route GET /:username i.e. /calebmcgowan
function user(req,res){
  var username = res.url.replace("/", "");
  if(username.length > 0){
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.write('Header\n');
    res.write(username + '\n');
    res.end('Footer\n');
  }
}

module.exports.home = home;
module.exports.user = user;

Based on what I understand, this appears to mean that the url value is not a string. What an I doing wrong here?

Jamie Reardon
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jamie Reardon
Treehouse Project Reviewer

Hi @Caleb McGowan, you are correct in that url is not a string data type, it is undefined therefore replace method is not accessible since it's a string method. Have you console logged the value of the res object to see what properties it does have?

Rich Donnellan
Rich Donnellan
Treehouse Moderator 27,708 Points

Question updated with code formatting. Check out the Markdown Cheatsheet below the Add an Answer submission for syntax examples, or choose Edit Question from the three dots next to Add Comment to see how I improved the readability.

1 Answer

Caleb McGowan
Caleb McGowan
5,289 Points

Figured it out.

var username = res.url.replace("/", ""); 

should have been

var username = req.url.replace("/", "");

The request / req is a string, while response / res wouldn't even let me type of it. Ended up being a simple typo

Rich Donnellan
Rich Donnellan
Treehouse Moderator 27,708 Points

Marked as "Best Answer" to close this out. There really should be a way for the OP to do this without giving them the points.