开发者

Express Static nodejs

There is a style.css in public, but I can't seem to make the express stat开发者_JS百科ic 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.


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


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
connect@1.6.1
express@2.4.4


app.use(express.static('public'));

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜