-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.js.code..txt
More file actions
41 lines (32 loc) · 853 Bytes
/
node.js.code..txt
File metadata and controls
41 lines (32 loc) · 853 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
node.js code
============
var app = require('express')();
var httpServer = require('http').Server(app);
//const cors = require('cors'); // for browsers
const port = 3000; // ignored by replit
const path = require('path');
var io = require("socket.io")(httpServer, {
cors: {
origin: "*",
methods: ["GET", "POST"]
}
});
// handle a browser
app.get('/', function(req, res) {
// res.send('Server is up and running');
res.sendFile(path.join(process.cwd(), "/html/index.html"));
console.log('got a get');
});
httpServer.listen(port, () => {
console.log('listening on *:' + port);
});
io.on('connection', (socket) => {
console.log('connect');
socket.on('chat_message', (msg) => {
io.emit('chat_message', msg);
console.log('message: ' + msg);
});
socket.on("disconnect", () => {
console.log("disconnect");
});
})