3

There is a style.css in public, but I can't seem to make the express static option work. I deleted express and have done npm install express, but still it isn't working. I just get a 404 error.

var express = require('express')
  , app = express.createServer();

app.use(express.static(__dirname+'/public'));

app.listen(8080, "127.0.0.1");

I have added an app.get() block to insure express is properly running, but I am still unable to request the static file.

3 Answers 3

33

An important detail that I have overlooked on multiple occasions now is that "public" is not in the URL of the static content you are delivering.

Alfred example nails it but it is easy to miss. The location of the README file is not localhost:8080/public/README, but simply localhost:8080/README

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

1 Comment

+1 Totally missed this point. I just spent an hour trying to figure out why everything from localhost:8080/public/ was giving me 404s. Thanks for the heads-up.
15

Works just fine for me.

app.js

var express = require('express')
  , app = express.createServer();

app.use(express.static(__dirname+'/public'));

app.listen(8080, "127.0.0.1");

mkdir public
cd public
touch README

README

test

$ curl http://localhost:8080/README
test

$ npm ls
[email protected]
[email protected]

2 Comments

reinstalled node.js and now everything is dandy, thanks. Maybe it was outdated or something? oh well working now.
Very good :). It could be outdated ;).. Node.js API changes, as well as express API...
1
app.use(express.static('public'));

http://expressjs.com/4x/api.html#express.static

1 Comment

if you want localhost:8080/public/ to work then you need to do: app.use('/public', express.static('public'));

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.