node.js 学习节点 - Express 公共文件夹不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24433733/
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
Learning Node - Express Public folder not working
提问by Tejs
So I'm new to node and trying to learn how to use the expresslibrary with it. However, the problem I'm trying to figure out is why the files in my /publicfolder do not seem to be served as static content.
所以我是 node 的新手,并试图学习如何使用express它的库。但是,我想弄清楚的问题是为什么我的/public文件夹中的文件似乎没有作为静态内容。
Here's my code:
这是我的代码:
var http = require('http');
var port = process.env.port || 1337;
var express = require('express');
var handlebars = require('express3-handlebars');
var path = require('path');
var application = express();
application.use(express.static(path.join(__dirname, 'public')));
application.engine('handlebars', handlebars({ defaultLayout: 'main' }));
application.get('/', function(req, res){
res.render('index.handlebars', { someProp: 3 });
});
application.listen(port);
And my directory structure:
还有我的目录结构:
/
- server.js (the above referenced file)
/ Views
- index.handlebars
/ Layouts
- main.handlebars
/ public
- ServeMe.txt
My understanding was that application.use(express.static(path.join(__dirname, 'public')));was supposed to configure the server to respond to any request under the public folder with that resource if found. What am I doing wrong? Funnily enough, it was easier to configure handlebars as the view engine than to get this public folder working =D
我的理解是application.use(express.static(path.join(__dirname, 'public')));应该配置服务器以响应具有该资源的公共文件夹下的任何请求(如果找到)。我究竟做错了什么?有趣的是,将把手配置为视图引擎比让这个公共文件夹工作更容易=D
EDIT: The full URL I am trying to request:
http://localhost:1337/public/serveme.txt
编辑:我试图请求的完整 URL:
http://localhost:1337/public/serveme.txt
I've tried case sensitivity (which should be a non-issue), and that also did not work.
我试过区分大小写(这应该不是问题),但这也不起作用。
回答by James Allardice
The full URL I am trying to request:
http://localhost:1337/public/serveme.txt
我试图请求的完整 URL:
http://localhost:1337/public/serveme.txt
That's your problem. Anything inside the directory you designate as static content is made available directly from the base URL. You need to request http://localhost:1337/serveme.txtinstead.
那是你的问题。您指定为静态内容的目录中的任何内容都可以直接从基本 URL 获得。您需要改为请求http://localhost:1337/serveme.txt。
If you want to only serve static files from /publicyou can pass a string as the first argument:
如果您只想提供静态文件,/public您可以传递一个字符串作为第一个参数:
application.use("/public", express.static(path.join(__dirname, 'public')));
回答by Dylan Kirkby
From the Express API reference
app.use(express.static(__dirname + '/public'));
This line serves your public folder at your app's root URL, meaning the following requests are valid and don't give a 404 error.
此行在您的应用程序的根 URL 处为您的公共文件夹提供服务,这意味着以下请求是有效的并且不会出现 404 错误。
// GET /javascripts/jquery.js
// GET /style.css
// GET /favicon.ico
// GET /javascripts/jquery.js
// GET /style.css
// GET /favicon.ico
app.use takes an optional first argument as well that allows you to specify which request URL you want to serve content at. This first argument defaults to "/", meaning your static content will be served at the base URL. If you want your static content to be served not at the root level, like your code currently does, check out the following example (again taken from the docs)
app.use 也接受一个可选的第一个参数,它允许您指定要提供内容的请求 URL。第一个参数默认为“/”,这意味着您的静态内容将在基本 URL 处提供。如果您希望您的静态内容不在根级别提供,就像您的代码当前所做的那样,请查看以下示例(再次取自文档)
app.use('/static', express.static(__dirname + '/public'));
This line serves your public folder again, but at your app's /static URL, making the following requests valid without a 404 error.
此行再次为您的公共文件夹提供服务,但在您的应用程序的 /static URL 中,使以下请求有效而不会出现 404 错误。
(Example URL: http://localhost:1337/static/anythingInYourPublicFolder)
(实施例URL: http://localhost:1337/static/anythingInYourPublicFolder)
// GET /static/javascripts/jquery.js
// GET /static/style.css
// GET /static/favicon.ico
// GET /static/javascripts/jquery.js
// GET /static/style.css
// GET /static/favicon.ico
回答by Peter Lyons
What URLs are you trying to load? You should notinclude "public" in your URLs, so curl -v 'http://localhost:1337/ServeMe.txt'.
您要加载哪些网址?你应该不包含在你的URL“公”,所以curl -v 'http://localhost:1337/ServeMe.txt'。

