JS文件获取了net :: ERR_ABORTED 404(未找到) [英] JS file gets a net::ERR_ABORTED 404 (Not Found)

查看:42
本文介绍了JS文件获取了net :: ERR_ABORTED 404(未找到)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的Io网络聊天.我最近想将html文件中的< script> 分隔为外部js文件.

I am trying to create a simple Io-web-chat. I recently wanted to seperate my <script> inside my html file to an external js file.

这是我非常简单的文件夹结构:

Chat
|-- index.html
|-- index.js
`-- server.js

html文件的相关部分:

<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script src="index.js"></script>
</body>
</html>

index.js文件的相关部分:

$(function() {

  //Initialize variables
  var $messageArea = $('#messages');
  var $InputMessage = $('#InputMessage');
  var $InputName = $('#InputName');

  //Initialize Socket
  var socket = io();

  //Send server Your message
  socket.emit('chat message', $InputMessage.val());

});

server.js文件的相关部分:

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

我还尝试将文件放入socket.io示例中具有的公共类型结构中:

I also tried putting my files in this public type structure that they have on the socket.io examples:

Chat
|-- Public
|   |-- index.html
|   `-- index.js
`-- server.js

在这种情况下,我更改了:html中的 src ="/index.js" /public/index.html 添加到server.js文件中但没有运气.

in that case I changed: src="/index.js" in html added /public/index.html into the server.js file But no luck.

所有这些都在localhost中运行.我在这里做什么错了?

This is all running in localhost. What am I doing wrong here?

推荐答案

:您需要一种将静态文件发送到客户端的方法.这可以通过Nginx之类的反向代理来实现,也可以简单地使用 express.static().

As mentionned in comments: you need a way to send your static files to the client. This can be achieved with a reverse proxy like Nginx, or simply using express.static().

将所有静态"(css,js,图像)文件放置在专用于其的文件夹中,这与放置视图"(此处是html文件)的位置不同.在示例中,我将其称为 static .完成后,在服务器代码中添加以下行:

Put all your "static" (css, js, images) files in a folder dedicated to it, different from where you put your "views" (html files in your case). I'll call it static for the example. Once it's done, add this line in your server code:

app.use("/static", express.static('./static/'));

这将通过/static路由有效地服务静态"文件夹中的每个文件.

This will effectively serve every file in your "static" folder via the /static route.

在客户端中查询您的index.js文件将变为:

Querying your index.js file in the client thus becomes:

<script src="static/index.js"></script>

这篇关于JS文件获取了net :: ERR_ABORTED 404(未找到)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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