简体   繁体   English

使用Node JS服务的页面在客户端加载.js文件

[英]Loading .js files on client side with page served by Node JS

I have the following simple JS file, which will look familiar to anyone who's used Socket.IO with NodeJS and the Express framework: 我有以下简单的JS文件,对于将Socket.IO与NodeJS和Express框架一起使用的任何人来说,它们都会看起来很熟悉:

var express = require('express'),
    app = express(),
    server = require('http').createServer(app),
    io = require('socket.io').listen(server);

server.listen(8374);

// routing
app.get('/', function (req, res) {
    res.sendfile(__dirname + '/index.html');
});

In index.html , I have the following line of code: index.html ,我有以下代码行:

<script src="/socket.io/socket.io.js"></script>

I have done some experimenting with pathnames and serving/mounting and I still don't really understand how this client-side line manages to work. 我已经做了一些路径名和服务/挂载的实验,但我仍然不太了解这个客户端线路是如何工作的。 The answer to this question says that by listening to the server , io handles all incoming Socket.IO requests. 这个问题的答案说,通过侦听serverio处理所有传入的Socket.IO请求。

... ...

My question is: Can this be done for other client-side JS files? 我的问题是:是否可以对其他客户端JS文件执行此操作?

For example, is there some easy way to bundle up JQuery so that it can be handled in the same way? 例如,是否有一些简单的方法来捆绑JQuery,以便可以用相同的方式处理它? At the moment I can put the file in a folder like public and use Express' app.use() method so that in index.html I can include this line: 目前,我可以将文件放在public类的文件夹中,并使用Express的app.use()方法,以便在index.html可以包含以下行:

<script src="/public/jquery-1.9.1.js"></script>

Is there a way to manage JQuery as a dependency as we can with NodeJS? 有没有一种方法可以像NodeJS一样将JQuery作为依赖项进行管理?

I'm thinking the end result would look something like this: 我认为最终结果将如下所示:

SERVER-SIDE: 服务器端:

var jquery = require('jquery');

CLIENT-SIDE: 客户端:

<script src="jquery/jquery-1.9.1.js"></script>

I'm not too sure about using modules to host specific files, but it would be more time-efficient to just host the file when it's requested: 我不太确定要使用模块来托管特定文件,但仅在请求时托管文件会节省更多时间:

app.get("/", function (req, res) {
    res.sendfile(__dirname + "/index.html");
});
app.get("/public/jquery-1.9.1.js", function (req, res) {
    res.sendfile(__dirname + "/public/jquery-1.9.1.js");
});

I don't use Express, so please excuse any mistakes. 我不使用Express,所以请原谅任何错误。

In express 2.x many used to use "dynamicHelpers" for this. express 2.x中,许多人为此使用“ dynamicHelpers”。 Something like this in your app.js 您的app.js中类似这样的内容

app.dynamicHelpers({
    scripts: function (){ 
      return ['/js/jquery.min.js', '/js/jquery.ui.min.js']
    }
})

In your layout using jade you'd do this. 在使用玉的布局中,您可以执行此操作。

- each s in scripts
    script(type='text/javascript', src= s)

Now app.dynamicHelpers has been removed so in express 3.x you'll need to either create a simple module for this or just do it inline. 现在,app.dynamicHelpers已被删除,因此在express 3.x中,您需要为此创建一个简单的模块,或者只是内联。 with app.use within your configuration or specific environment if need be. 如果需要,可在您的配置或特定环境中使用app.use。 something like. 就像是。

app.use(function (req, res, next){
   res.locals.scripts = ['/js/yourscript.js', '/js/yourotherscript.js']
   next();
}

Using it in the jade layout would be the same. 在玉器布局中使用它会是相同的。 If I'm understanding you correctly that would be one way to include these scripts. 如果我理解正确,那将是包含这些脚本的一种方法。 Personally I'd rather included them statically though. 我个人宁愿静态地包括它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM