Skip to content

Instantly share code, notes, and snippets.

@smpnjn
Created May 22, 2021 18:23
Show Gist options
  • Save smpnjn/eb4869ff29f3b1ea821d155ccf5da420 to your computer and use it in GitHub Desktop.
Save smpnjn/eb4869ff29f3b1ea821d155ccf5da420 to your computer and use it in GitHub Desktop.
import path from 'path'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
import express from 'express'
import expressWs from 'express-ws'
import http from 'http'
// Our port
let port = 3000;
// App and server
let app = express();
let server = http.createServer(app).listen(port);
// Apply expressWs
expressWs(app, server);
app.use(express.static(__dirname + '/views'));
// Get the route /
app.get('/', (req, res) => {
res.status(200).send("Welcome to our app");
});
// Get the /ws websocket route
app.ws('/ws', async function(ws, req) {
ws.on('message', async function(msg) {
console.log(msg);
// Start listening for messages
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment