即使使用express.static,NodeJS也不会提供静态文件 [英] NodeJS won't serve static files, even when using express.static

查看:321
本文介绍了即使使用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

这篇关于即使使用express.static,NodeJS也不会提供静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆