Kit Sunde Kit Sunde - 1 year ago 132
Node.js Question

Express-js can't GET my static files, why?

I've reduced my code to the simplest express-js app I could make:

var express = require("express"),
app = express.createServer();
app.use(express.static(__dirname + '/styles'));

My directory look like this:


Yet when I access
I get the following error:

Cannot GET / styles /

I'm using
express 2.3.3
node 0.4.7
. What am I doing wrong?

Answer Source

Try http://localhost:3001/default.css.

To have /styles in your request URL, use:

app.use("/styles", express.static(__dirname + '/styles'));

Look at the examples on this page:

//Serve static content for the app from the "public" directory in the application directory.

    // GET /style.css etc
    app.use(express.static(__dirname + '/public'));

// Mount the middleware at "/static" to serve static content only when their request path is prefixed with "/static".

    // GET /static/style.css etc.
    app.use('/static', express.static(__dirname + '/public'));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download