如何在Node.js,ExpressJS中使用外部JS文件 [英] How to use an external js-File in Nodejs, ExpressJS
问题描述
我今天开始使用NodeJS,ExpressJS,Android创建一个小的Web应用程序.而且我遇到了一个问题,它整日困扰着我!
I started today with NodeJS, ExpressJS, Android, to create a little Webapplication. And I faced a problem an it holds me the whole day!
ENV:
服务器:NodeJS
,ExpressJS
客户端:HTML
,Angular
,CSS
我尝试从node_modules文件夹加载CSS和JavaScript文件.但这是行不通的.它仅适用于在线资源
I try to load the CSS and the JavaScript file from the node_modules folder. But it doesn't work. It works just with the online sources
有效
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../public/javascript/test.js"></script>
不起作用
<script type="text/javascript" src="../public/javascript/test.js"></script>
服务器始终写入404错误
The server write all the time a 404 Error
我已经尝试过绝对和相对路径. 我也尝试过其他stackoverflow帖子中的服务器建议.没有结果.
I've tried with absolut and relativ path. I've tried also with serveral sugesstions from other stackoverflow posts. without a result.
服务器代码是由Eclipse IDE使用"Express JS Project"创建的.
The Servercode was created by Eclipse IDE with 'Express JS Project'
app.js
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path')
,pageX = require('./routes/pageX');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/pageX', function(req,res){
console.log("TEST - PageX");
res.send(pageX.pageX(req, res));
});
app.get('/users', user.list);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
迈克尔
推荐答案
根据 docs (使用此功能时):
According to the docs, when using this:
app.use(express.static(path.join(__dirname, 'public')));
Express查找相对于静态目录的文件,因此静态目录的名称不是URL的一部分.
Express looks up the files relative to the static directory, so the name of the static directory is not part of the URL.
因此,要加载test.js
,您应该使用:
So, to load your test.js
you should use:
<script type="text/javascript" src="/javascript/test.js"></script>
此外,这意味着您应该在__dirname
下具有一个public
目录.
Also, it means that you should have a public
directory under __dirname
.
如果我正确地理解了您,并且按照您到目前为止说过的话,您正在node_modules
内部运行node
,并且您具有以下目录结构:
If I understood you correctly and from what you said you've tried so far, you are running node
from inside the node_modules
and you have the following directory structure:
app-root-directory/
public/
node_modules/
我对节点也很陌生,我相信您不应该从node_modules
内部运行.但是,如果您不是path.join(__dirname, 'public')
,那么它不是您想要的,因为它将指向node_modules
中的public
.
I'm quite new to node as well and I believe you're not supposed to be running from inside node_modules
. But if you are then path.join(__dirname, 'public')
is not what you want as it will point to public
inside node_modules
.
因此,您还必须将express.static
的根目录更改为../public
:
So you also have to change the root directory of express.static
to ../public
:
app.use(express.static(path.join(__dirname, '../public')));
这篇关于如何在Node.js,ExpressJS中使用外部JS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!