1

I'm looking to use node.js with express to create a Webserver that serves static files (as opposed to using something like Apache). This is what I have so far:

var express = require('express');
var app = express();
var http = require('http');
var path = require('path');

app.configure(function() {
    app.use(express.static(path.join(__dirname, '/public')));
    app.use(app.router);
});

var server = http.createServer(app);

server.listen(4001);

This works in that anything placed in the public folder can be accessed via browser. I have two questions though.

  1. How do I make it so that node.js will provide a 404 error when a user tries to access a non-existent file?
  2. How would I then select my homepage?

2 Answers 2

4

Take a look at existing implementations like ecstatic and http-server.

Sign up to request clarification or add additional context in comments.

Comments

0

you can have a homepage using a /public/index.html file you can create a 404 page by adding after app.router

app.use(function (req, res) {
  res.send(404, 'File not found.')
})

or you can use res.render or whatever you'd like.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.