我已经将我的代码简化为我可以做的最简单的express-js应用程序:
var express = require("express"),
app = express.createServer();
app.use(express.static(__dirname + '/styles'));
app.listen(3001);
我的目录是这样的:
static_file.js
/styles
default.css
然而,当我访问http://localhost:3001/styles/default.css时,我得到以下错误:
Cannot GET / styles /
default.css
我使用express 2.3.3和节点0.4.7。我做错了什么?
如果你的设置
myApp
|
|__ public
| |
| |__ stylesheets
| | |
| | |__ style.css
| |
| |___ img
| |
| |__ logo.png
|
|__ app.js
然后,
放入app。js
app.use('/static', express.static('public'));
并引用你的style.css:(在一些.pug文件中):
link(rel='stylesheet', href='/static/stylesheets/style.css')
这个方法对我很管用
app.use(express.static(path.join(__dirname, 'public')));
app.use('/img',express.static(path.join(__dirname, 'public/images')));
app.use('/shopping-cart/javascripts',express.static(path.join(__dirname, 'public/javascripts')));
app.use('/shopping-cart/stylesheets',express.static(path.join(__dirname, 'public/stylesheets')));
app.use('/user/stylesheets',express.static(path.join(__dirname, 'public/stylesheets')));
app.use('/user/javascripts',express.static(path.join(__dirname, 'public/javascripts')));