Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Being new to Node.js, I'd like to know of a better way of implementing this:

server = http.createServer( function(req, res) {
if (req.url === '/') {
        fs.readFile('index.html', function(err, page) {
            res.writeHead(200, {'Content-Type': 'text/html'});
            res.write(page);
            res.end();
        });
}   
 else if (req.url == '/new.html') { 
        fs.readFile('new.html', function(err, page) {
                res.writeHead(200, {'Content-Type': 'text/html'});
                res.write(page);
                res.end();
        });
}
});
server.listen(8080);

Using something like this:

var Url = require('url')
...

// In request handler
var uri = Url.parse(req.url);
switch (uri.pathname) {
  case "/":
    ..
  case "/new.html":
    ..
}
share|improve this question
Use a router like express. – Raynos Dec 31 '11 at 17:33
Other than using a framework... – Simpleton Dec 31 '11 at 23:30
If you want static routing you can write a naive static router – Raynos Jan 1 '12 at 10:48

1 Answer

up vote 2 down vote accepted

Whether you wanna implement a static server? If so, you can just use connect and its built-in static middlewave. With it, you just need to write the following code to implement a static server:

var connect = require("connect");

connect(
    connect.static(__dirname)
).listen(3000);

That's it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.