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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Caleb McGowan](https://secure.gravatar.com/avatar/c4857599e4954abe2edb42a7f6897b35?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Caleb McGowan
5,288 PointsTypeError: 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?
![Rich Donnellan](https://secure.gravatar.com/avatar/7710b15be09365c14d0805863ac55114?s=60&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Rich Donnellan
Treehouse Moderator 27,696 PointsQuestion 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](https://secure.gravatar.com/avatar/c4857599e4954abe2edb42a7f6897b35?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Caleb McGowan
5,288 PointsFigured 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](https://secure.gravatar.com/avatar/7710b15be09365c14d0805863ac55114?s=60&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Rich Donnellan
Treehouse Moderator 27,696 PointsMarked as "Best Answer" to close this out. There really should be a way for the OP to do this without giving them the points.
Jamie Reardon
Treehouse Project ReviewerJamie Reardon
Treehouse Project ReviewerHi @Caleb McGowan, you are correct in that
url
is not a string data type, it isundefined
thereforereplace
method is not accessible since it's a string method. Have you console logged the value of theres
object to see what properties it does have?