I am trying to deploy my app or API on shared hosting, but I keep on encountering an error that says Error: http.Server.listen() was called more than once which is not allowed.
I have no idea why this is happening my whole code have just a single server.listen
and one app.listen
this is my server.js code --
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.
this is how i am creating the application
extra information- Node.js version on my local machine is 20.9.0 but I could find only the closest that is 20.12.2/19.9.0
it works great on my local machine and I have just a single server.listen
in my whole server code as I have given already
doesn't matter I select development mode or production it gives same error
This worked for me by separating server listening code (in-your-case app.listen) in separate file rather than keeping in the same server.js file. PS: my code didn't have web socket server, anyways, u can give it a try. Let me know if this works for you :)