gpt4 book ai didi

Node.js 聊天应用程序与 Socket.io 和 Express 在本地工作但不在托管上工作

转载 作者:太空宇宙 更新时间:2023-11-04 00:06:13 26 4
gpt4 key购买 nike

我制作了 socket.io 的“入门”示例,它在我的计算机上的本地主机上完美运行。但是当我将它上传到我的主机时它不起作用,客户端在尝试获取 socket.io.js 时抛出 404 错误:

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

我用其他引用解决了这个问题(我不需要在本地主机上执行此操作):

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js"></script>

但是,当客户端在主机上运行时,我不断收到 404 错误。 Node.js 服务器似乎没有在服务器上运行,并且在托管时不提供 socket.io 文件。当我在 localhost 上时,我没有收到这些 404 错误。有一个这样调用的示例:

GET http://localhost/socket.io/?EIO=3&transport=polling&t=MMc5E4X (200 在本地工作正常)

GET http://tinatest.gearhostpreview.com/socket.io/?EIO=3&transport=polling&t=MMc5E4X (404)

我在免费的 Node.js 托管上获得了这个示例,以便您可以查看它,我在此服务器上激活了 Node.js 8,它在端口 80 上运行:

http://tinatest.gearhostpreview.com/

服务器上的文件是:

Index.html

<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>

</style>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(function () {
var socket = io();
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
});
</script>
</html>

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(80, function(){
console.log('listening on *:80');
});

package.json

{
"name": "socket-chat-example",
"version": "0.0.1",
"description": "my first socket.io app",
"dependencies": {
"express": "^4.15.2",
"socket.io": "^2.1.1",
"socket.io-client": "^2.1.1"
}
}

我怎样才能做到这一点?或者如果不可能,为什么不可能?

最佳答案

考虑一下 Node.js + express 服务器的作用。其中,它可以根据各种类型的 http 请求提供文件服务。但是,您必须准确地告诉它要监听哪些类型的请求(GETPOST 等)以及在哪些路由上(//文件夹)。您甚至可以监听查询字符串、参数、发布消息的正文等。但是,由您来定义它们。

看看你的代码,你定义了什么路由?

只有一个:/ 并且您允许对该路由发出 GET 请求,作为响应,您将提供 index.html,其中包含所有内容至少在本例中是前端逻辑的一部分。

如果您尝试 GET 其他路由,例如 /socket.io/,默认情况下您会收到 404 错误。

您提到的脚本文件具有 src/socket.io,因为您没有服务 socket.io 代码的路由,所以它失败在生产中。

在本地主机上,如果文件在同一项目中可用,则浏览器可以解析本地计算机上的相对路径。项目部署后,相对路径不再查找您的计算机,而是使用完整的 url 访问您的路由器。它期望提供这些文件。

您可以通过为必要的 socket.io 代码提供完整的 url 引用来解决生产中的问题。

更多:示例:

index.js - 请参阅下面的评论

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

/**
* Your ROUTES
* in production, when someone goes to http://tinatest.gearhostpreview.com/
* they are calling your root route "/"
* Your callback function is sending back index.html from your root directory
* You add __dirname because you don't know where gearhost is actually putting
* your stuff.
*
* In production, you can only access server-side files if you serve them
*/
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});

/**
* ONE WAY TO SERVE JS TO YOUR CLIENT
* (there are many better ways of doing this)
*
* If you do this, you will not get a 404 error
*/
app.get('/socket.io/socket.io.js', function(req, res){
// this assumes you have a folder within your root named socket.io
// and that it contains socket.io.js,
res.sendFile(__dirname + '/socket.io/socket.io.js');
})


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

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

index.html - 仅查看脚本

<!-- 
The src is the URI of an external script, that is,
a script external to this particular document.

Below, the URIs are absolute paths for getting for files hosted by other services.
-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>

<!--
You can also use relative paths.

If your src value is /socket.io,
then your page looks for a file
relative to the current location of itself.

On localhost, your page has access to files within its own folder
-->

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

<!--
HOWEVER, in production, you cannot use relative paths
UNLESS you are serving your files either as static assets

via middleware (http://expressjs.com/en/guide/using-middleware.html)
OR via a public route (http://expressjs.com/en/4x/api.html#app.use)

You are getting a 404 in production because you aren't serving
assets.

BUT!!! you can always use an external 3rd party service
-->

关于Node.js 聊天应用程序与 Socket.io 和 Express 在本地工作但不在托管上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52173179/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com