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

Nikita Voloboev
Nikita Voloboev
4,816 Points

After refreshing the page, the server stops working, why?

Here is what is outputted to the console : http://i.imgur.com/xGRvBa3.png

Here is my app.js file :

var router = require("./router.js")
// problem : we need a simple way to look at user's badge count and JavaScript point from a web browser
// solution : use node.js to perfom the profile look ups and server our template via HTTP

// create a web server 
var http = require('http')
http.createServer(function (request, response) {
    router.home(request, response)
    router.user(request, response)
}).listen(3000)
console.log('server running at http://127.0.0.1:3000/')


// function that handles the reading of files and merge in value
//    read from file and get a string 
//      merge values in to string

And here is my router.js code :

var Profile = require("./profile.js")
// handle HTTP route GET / and POST / ie Home
function home(request, response) {
// if url == "/" && GET 
if (request.url === "/") {

//    show search
    response.writeHead(200, {'Content-Type': 'text/plain'})
    response.write("Header\n")
    response.write("Search\n")
    response.end('Footer\n')

// if url == "/" && POST 
//    redirect to /:username 
}
}

// handle HTTP route GET /:username ie /chalkers
function user(request, response) {
// if url = "/...."
var username = request.url.replace("/", "")
    if (username.length > 0) {
    response.writeHead(200, {'Content-Type': 'text/plain'})
    response.write("Header \n")
    response.write(username + "\n")
    response.end('Footer\n')
    }
//  get json from Trehouse
//    on "end"
//      show profile 
//    on "error" 
//      show error
}

module.exports.home = home
module.exports.user - user

Thank you for any help.

1 Answer

Thomas Nilsen
Thomas Nilsen
14,957 Points

I just scanned through the code really quickly.

Only thing the jumped out was the following:

module.exports.home = home
module.exports.user - user

Last line should be '=' instead of '-'