Hello, I seem to be having a problem when it comes to my project. My project uses NPM to run. After doing a commit previous commits don’t work anymore, and the project fails on deployment with no answers as to why. Here is the code if it helps:
const express = require('express');
const pug = require("pug");
const path = require("path");
const routes = require("./routes/routes");
const webSocket = require('ws');
const socketServer = require('ws').Server;
const portNum = process.env.PORT || 3000
const app = express();
//Set up view engine
app.set("view engine", 'pug');
app.set("views", __dirname + '/views');
app.get('/', routes.index);
//Do this because websockets don't work the same.
const server = app.listen(portNum);
const wss = new socketServer({ server });
console.log("Running on port: " + portNum);
wss.on('connection', (ws) => {
console.log('[Server] A client has connected.');
ws.on('close', () => { console.log('[Server] Client disconnected') });
ws.on('message', (message) => {
console.log('[Server] Recieved message: %s', message)
//Broadcast to everybody else. Sending to the same user may cause recursion.
wss.clients.forEach(function each(client) {
if (client !== ws && client.readyState === webSocket.OPEN) {
client.send(JSON.stringify(message));
}
});
})
})