Javascript nodejs/express 包含本地 js 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29357305/
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
nodejs/express include local js file
提问by NorCalKnockOut
Here is my current folder structure
这是我当前的文件夹结构
css
app.css
js
app.js
node-modules
index.html
node-server.js
package.json
The node-server is hosting index.html, but I can't figure out how to get the app.jsand app.cssfiles to get loaded.
节点服务器正在托管index.html,但我不知道如何加载app.js和app.css文件。
index.htmlloads them with:
index.html加载它们:
<script src="js/app.js"></script>
<link rel="stylesheet" type="text/css" href="css/app.css"/>
Here is the error message:
这是错误消息:
Failed to load resource: the server responded with a status of 404 (Not Found)
2http://localhost:3000/css/app.css Failed to load resource: the server
responded with a status of 404 (Not Found)
I know i need to require or load module or something, just can't figure out what.
我知道我需要要求或加载模块或其他东西,只是不知道是什么。
Thanks
谢谢
回答by Nivesh
ReasonNode.Js does not server static content on it's own, routes has to defined for serving static content via Node.
原因Node.Js 不自行提供静态内容,必须定义路由以通过 Node 提供静态内容。
Solution(Manual):
解决方案(手动):
var express = require('express'),
path = require('path'),
app = express();
app.get('/index.html',function(req,res){
res.sendFile(path.join(__dirname + '/index.html'));
});
app.get('/css/app.css',function(req,res){
res.sendFile(path.join(__dirname + '/css/app.css'));
});
app.get('/js/app.js',function(req,res){
res.sendFile(path.join(__dirname + '/js/app.js'));
});
app.get('/', function(req, res) {
res.redirect('index.html');
});
app.listen(8080);
Better Solution:
更好的解决方案:
Directory Structure:
目录结构:
public
css
app.css
js
app.js
index.html
CODE:
代码:
var express = require('express'),
path = require('path'),
app = express();
// Express Middleware for serving static files
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req, res) {
res.redirect('index.html');
});
app.listen(8080);
回答by James Taylor
As Tomasz Kasperekpointed out, you need to let Express know that you intend to host these files in a static directory. This is technically called defining static middleware.
正如Tomasz Kasperek指出的那样,您需要让 Express 知道您打算将这些文件托管在静态目录中。这在技术上称为定义静态中间件。
This should look something like:
这应该类似于:
var express = require('express');
var app = express();
// first parameter is the mount point, second is the location in the file system
app.use("/public", express.static(__dirname + "/public"));
It's super simple and I suggest you go the route of making some sort of publicfolder, rather than bothering to make specific files and folders static.
这非常简单,我建议您采用创建某种public文件夹的方法,而不是费心将特定文件和文件夹设为静态。
Then the files would simply be referenced like so from the root index.html:
然后这些文件将被简单地从根引用index.html:
<link href="public/css/reset.css" rel="stylesheet" type="text/css">
Hope this helps you!
希望这对你有帮助!
回答by ajup
I got it to work by using this syntax
我通过使用这种语法让它工作
app.use(express.static('public'));
Copy the css and js files under the 'public' directory and then add the reference in the index.html file
复制'public'目录下的css和js文件,然后在index.html文件中添加引用
<link rel="stylesheet" href="/css/reset.css">

