我正在尝试在共享托管上部署我的应用程序或 API,但我不断遇到错误,提示
Error: http.Server.listen() was called more than once which is not allowed.
我不知道为什么会发生这种情况,我的整个代码只有一个
server.listen
和一个 app.listen
这是我的 server.js 代码 --
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const routes = require('./routes/route');
const essentials = require('./essentials');
const admin = require('./firebaseAdminSDK');
const http = require('http');
const WebSocket = require('ws');
require('dotenv-safe').config();
const { SECRET_KEY } = process.env;
const AUTH = admin.auth();
const DB = admin.firestore();
const STORAGE = admin.storage();
const userConnections = new Map();
const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
const PORT = process.env.PORT || 5000;
app.use(bodyParser.json({ limit: '10mb' }));
app.use(bodyParser.urlencoded({ limit: '10mb', extended: true }));
app.use(cookieParser());
app.use('/', routes); app.listen(PORT, () => {
console.log('Server is listening on Port:', PORT);
});
server.listen(8000 () => {
console.log('websocket server started on port 8000');
});
...there is more code below around of 400 lines but that is not related to it this is the main code.
这就是我创建应用程序的方式
额外信息- 我本地计算机上的 Node.js 版本是 20.9.0,但我只能找到最接近的版本,即 20.12.2/19.9.0
它在我的本地计算机上运行得很好,而且我的整个服务器代码中只有一个
server.listen
,正如我已经给出的那样
无论我选择开发模式还是生产模式,它都会给出相同的错误
我遇到了同样的问题,因为这是我第一次尝试通过 cpanel 部署我的节点应用程序。你找到解决办法了吗??如果是,我们将非常感谢您的帮助。