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

NodeJS sample code runs differently than in course presentation

Course 'Build a Simple Dynamic Site with Node.js' with Andrew Chalkers. I spent hours trying to figure out the problem and simply I have no clue what is happening. Here is trivial code:

app.js

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

var http = require('http');

http.createServer(function(request, response){

    router.home(request, response);

    router.user(request, response);

}).listen(3000);

router.js

function homeRoute(request, response) {
    response.writeHead(200, {'Content-Type' : 'text/plain'});
    if(request.url === '/') {
        response.write('Header\n');
        response.write('Search\n');
        response.end('Footer\n');
    }
}

function userRoute(request, response) {

    response.writeHead(200, {'Content-Type' : 'text/plain'});

    var username = request.url.replace('/', '');

    if ( username.length > 0 ) {

        response.write('Header\n');

        console.log("after header for [" + username + "]");

        response.write('got it for [' + username + ']' + '\n');

        response.end('Footer\n');

    }
}

module.exports.home = homeRoute;
module.exports.user = userRoute; 

and then when I run "node app.js" and type url: http://localhost:3000/chalkers I am getting this console log:

after header for [chalkers]

after header for [favicon.ico]

it is as if someone right after I send my url kept issuing some url with /favicon.ico. The problem is that when I try call user profile on user favicon.ico it crashes the server.

Anybody knows where this 'favicon.ico' comes from?

Thanks Cezar

I haven't done that particular course yet, but if you're able to post a workspace snapshot I can take a look and see if we can't figure out what's causing the snag.

1 Answer

Hi Cezar,

The 'favicon.ico' is an icon (image) file used by many sites to display a small image by the site url in the address bar of your browser. It's not a user.

From what I can gather, node automatically requests this file on get requests. (There's actually a npm package called serve-favicon, which allows you to get around this.)

That said, I just tested your files on my localhost, using node 4.2.1, and it's running correctly without making requests to favicon.ico. However, I haven't been able to confirm if this is because node 4.x has corrected the issue; I assume that Treehouse's version is older than 4.