即使使用express.static,NodeJS也不会提供静态文件
问题描述:
我有以下一些内容:
var request = require('request'),
express = require('express');
var app = express.createServer();
var port = process.env.PORT || 8080;
app.configure(function(){
app.set("view options", { layout: false, pretty: true });
app.use(express.favicon());
app.use("/public", express.static(__dirname + '/public'));
}
);
app.listen(port);
// Routes
app.get('/', function(req, resp){
resp.render('index.jade', {pageTitle: 'Some title'});
});
然而,当我访问/public/myfile.css例如,我仍然得到:
Yet, when I visit /public/myfile.css for example, I still get:
无法GET /public/myfile.css
我的index.jade模板似乎无法调用文件
Cannot GET /public/myfile.css My index.jade templates cannot seem to call the files either
为什么是这样?
答
我不认为提供支持的路径:
I don't think supplying the path like that is supported:
app.use("/public", express.static(__dirname + '/public'));
尝试这个,并在根目录中查找您的公共文件:
Try this, and look for your public files in the root:
app.use(express.static(__dirname + '/public'));
所以 /public/myfile.css
/myfile.css
。