节点没有找到包含在html中的js文件 [英] Node is not finding js files included in html

查看:66
本文介绍了节点没有找到包含在html中的js文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用node和socket.io构建一个简单的聊天应用程序。我正在按照此处列出的教程进行操作: http://socket.io/get-started/chat/



我的问题是,本教程中有一些JavaScript直接放在html中的脚本标记中。我想将这些代码移到它自己的js文件中。

我制作了一个名为chat.js的文件,它位于我的index.html和index.js所在的目录中。在我的html中,我把下面的代码放在标题中(我也在结束标记之前尝试过)

 <脚本type =text / javascriptsrc =chat.js>< / script> 

但是,当我在终端运行node index.js并转到localhost时, chat.js.我尝试过放置/chat.js以及./chat.js而没有运气。所有这三个文件都在同一个目录中。



任何线索我做错了将不胜感激。



我的index.js

  var app = require('express')(); 
var http = require('http')。Server(app);
var io = require('socket.io')(http);

app.get('/',function(req,res){
res.sendFile(__ dirname +'/index.html');
});

$ b io.on('connection',function(socket){
socket.on('chat message',function(msg){
io.emit ('chat message',msg);
});
});
$ b http.listen(3000,function(){
console.log('listen on *:3000');
});


解决方案

node.js不会自动提供其他文件网络服务器呢。如果您希望在浏览器请求时发送 chat.js ,则必须在node.js代码中为其创建路由,以便Web服务器将发送如果你使用类似Express框架的东西,这可以用一行代码来完成,代码如下: app.use(express.static (...))



请注意,在您链接的演示中, / 路径。对于 /chat.js 您需要类似的路线,或者您可以使用 app.use(express.static(...))

将来,如果您显示实际的服务器代码,那么我们可以更具体地帮助实际的服务器代码代码适合你的服务器。






现在你已经显示了你的代码,你可以为 /chat.js

  app.get('/ chat.js ',function(req,res){
res.sendFile(__ dirname +'/chat.js');
});

或者,如果您将chat.js移动到 public 子目录下,然后你可以用这个自动提供该目录中的所有文件:

  app.use(express.static( '公共')); 

当Express获得一个没有特定处理程序的请求时,它会检查公共子目录查看文件是否与请求名称匹配。如果是这样,它会自动提供该文件。


I'm trying to build a simple chat app using node and socket.io. I am following the tutorial listed here: http://socket.io/get-started/chat/

My issues is that the tutorial has some javascript that is placed in a script tag directly in the html. I would like to move this code into it's own js file.

I made a file called chat.js, that is in the same directory as my index.html and index.js. In my html I put the following code in the header (I also tried right before the ending body tag too)

  <script type="text/javascript" src="chat.js"></script>

However, when I run node index.js in terminal and go to localhost, I get a 400 for chat.js. I've tried placing "/chat.js" as well as "./chat.js" with no luck. All three files are in the same directory.

Any clues to what I am doing wrong will be appreciated.

My index.js

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

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


io.on('connection', function(socket){
 socket.on('chat message', function(msg){
   io.emit('chat message', msg);
 });
});

http.listen(3000, function(){
 console.log('listening on *:3000');
});

解决方案

node.js does not automatically serve any files like other web servers do. If you want it to send chat.js when the browser requests it, you will have to create a route for it in your node.js code so that the web server will send it.

If you use something like the Express framework, this can be done in perhaps one line of code with app.use(express.static(...)).

Notice how in the demo you linked to, there's a specific route for the / path. You need a similar route for /chat.js or you could use app.use(express.static(...)) to configure the automatic serving of a whole directory of files.

In the future, if you show your actual server code, then we could help more specifically with actual code that fits into your server.


Now that you've shown your code, you could add a specific route for /chat.js:

app.get('/chat.js', function(req, res){
    res.sendFile(__dirname + '/chat.js');
});

Or, if you move chat.js to be in a public sub-directory under your app code, then you could serve all files in that directory automatically with this:

app.use(express.static('public'));

When Express gets a request for a route that doesn't have a specific handler, it will check the public sub-directory to see if a file matches the request name. If so, it will automatically serve that file.

这篇关于节点没有找到包含在html中的js文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆