Javascript Express 静态 nodejs
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7069360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Express Static nodejs
提问by Lime
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.
有一个style.css
in public
,但我似乎无法使快速静态选项起作用。我删除express
并完成了npm install express
,但仍然无法正常工作。我只是收到 404 错误。
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.
我添加了一个app.get()
块以确保 express 正常运行,但我仍然无法请求静态文件。
回答by user1452437
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.
我现在多次忽略的一个重要细节是“公开”不在您提供的静态内容的 URL 中。
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
阿尔弗雷德的例子说明了这一点,但很容易错过。README 文件的位置不是 localhost:8080/public/README,而是 localhost:8080/README
回答by Alfred
Works just fine for me.
对我来说很好用。
app.js
应用程序.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]
回答by zavidovych
app.use(express.static('public'));