gpt4 book ai didi

node.js - heroku 上的 socket.io 请求 404

转载 作者:太空宇宙 更新时间:2023-11-03 23:39:14 26 4
gpt4 key购买 nike

我的应用程序在本地工作,但是当我部署到heroku时,对myapp.herokuapp.com/socket.io的请求得到404响应。

这是我的代码:

var express = require('express');
var http = require('http');
var app = express();
var port = 8080;

// Configuration of the server
require('./ServerConfig').serverConfig(app, express);


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

io.on('connection', function(socket){
console.log('a user connected');
socket.on('message', function(msg){
console.log(msg);
io.emit('message', msg);
});
});

我刚刚开始使用nodejs,我不太明白它为什么崩溃。

有人可以解释一下这个错误的原因或如何调试它吗?

谢谢!

最佳答案

将第 4 行替换为:

var port = process.env.PORT || 8080;

在 Heroku 上,您应该绑定(bind)到 PORT 环境变量。看: https://devcenter.heroku.com/articles/runtime-principles#web-servers

关于node.js - heroku 上的 socket.io 请求 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26821534/

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