-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
55 lines (48 loc) · 1.41 KB
/
index.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const express = require('express');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http, {
cors: {
origin: 'http://localhost:3000',
methods: ['GET', 'POST'],
credentials: true
}
});
const moment = require('moment');
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000');
next();
});
app.use(express.static('public')); // Serve static files from the 'public' directory
// json { "status": "", [ "timestamp": "", "username": "", "subreddit": "", "type": "", "body": "" ] }
const data = {
status: "unread",
timestamp: moment().format(),
username: "Alice",
subreddit: "r/aww",
type: "comment",
body: "This is a test comment"
};
const mess = {
id: 1,
sender: "Aliceooasods",
subject: "Hello",
message: "Hello, world!",
timeSent: "2019-01-01T12:00:00"
};
app.post('/messages', (req, res) => {
// Handle POST requests to '/messages' here
console.log('Received a message:', req.body);
res.sendStatus(200);
});
io.on('connection', (socket) => {
console.log('A user connected!');
setInterval(() => {
socket.emit('receiveNotification', data);
console.log('Notification sent to client!')
}, 1000);
});
const port = process.env.PORT || 4000;
http.listen(port, () => {
console.log(`Server listening on port ${port}`);
});