简体   繁体   English

我如何在node.js应用程序中包含JavaScript文件?

[英]how can i include JavaScript file in node.js app?

i am accessing the script.js and libs.js in index.html like this: 我正在访问index.html中的script.js和libs.js,如下所示:

<body style='margin:0px' >
<canvas id='your_canvas'
        style='position: absolute; background-color: black;'></canvas>

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="/libs.js"></script>
<script type="text/javascript" src="/script.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
    jQuery(function($){

        main(); //this function is inside of script.js

    });
</script>

but the .js files cannot be accessed and showing error: 但无法访问.js文件并显示错误:

1) GET script.js 404 (Not Found) localhost/:12. 1)GET script.js 404(Not Found)localhost /:12。
2) GET libs.js 404 (Not Found) localhost/:11. 2)获取libs.js 404(未找到)localhost /:11。
3) Uncaught ReferenceError: main is not defined (index):17. 3)未捕获的ReferenceError:未定义main(索引):17。

where am i going wrong and how can i include these two JavaScript files using node.js? 我哪里出错了,如何使用node.js包含这两个JavaScript文件?

my app.js is : 我的app.js是:

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

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

app.use(express.static(__dirname + '/public/script.js'));
app.use(express.static(__dirname + '/public/libs.js'));

 io.sockets.on('connection', function(socket) {
    socket.on('send coordinates',function(data){
    io.sockets.emit('coordinates', data);
    });

}); });

and then i have my script.js and libs.js files. 然后我有我的script.js和libs.js文件。

You need to actually serve these files if you want them to be accessible. 如果您希望这些文件可以访问,则需要实际提供这些文件。 You can't just include them in your project directory. 您不能只将它们包含在项目目录中。 If you are using Express , you can designate a static file directory like this: 如果您使用的是Express ,则可以指定一个静态文件目录,如下所示:

app.use(express.static(__dirname + '/public'));

You can also use node-static or even create a handler for each file... 您还可以使用node-static甚至为每个文件创建一个处理程序......

Using express, your code should look something like this (Not tested): 使用express,您的代码应该看起来像这样(未测试):

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

app.get('/', function(req, res){
    res.sendfile(__dirname + '/public/index.html'); // I would move this file to public
});

app.use(express.static(__dirname + '/public'));

io.sockets.on('connection', function(socket) {
    socket.on('send coordinates',function(data){
        io.sockets.emit('coordinates', data);
    });
});

app.server.listen(8000);

Are you trying to call: 你打算打电话:

<script>
jQuery(function($){

    main(); //this function is inside of script.js

});
</script>

If you are then you need to save your code as a file named script.js in the root directory. 如果您那么需要将代码保存为根目录中名为script.js的文件。 Adding javascript/jQuery between tags in your html does not need to be called and will run automatically. 在html中的标签之间添加javascript / jQuery不需要被调用并且将自动运行。

I'd advise laying your script out in the following way too, i don't recognize the way you have laid it out. 我建议你也按照以下方式编写脚本,我不承认你的方式。

<script>

var main = function() {
    \\Your javascript/jQuery
}

$(document).ready(main);

</script>

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

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