Created
May 22, 2021 18:23
-
-
Save smpnjn/eb4869ff29f3b1ea821d155ccf5da420 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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