-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
33 lines (27 loc) · 983 Bytes
/
server.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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors')
const app = express();
const PORT = 3000;
// Enable CORS for both origins
app.use(cors({
origin: ['http://localhost:5500', 'http://127.0.0.1:5500', 'localhost:5500', '127.0.0.1:5500']
}));
app.use(bodyParser.json());
const messages = [];
app.get('/messages', (req, res) => {
const lastIndex = parseInt(req.query.lastIndex, 10) || 0;
const newMessages = messages.slice(lastIndex);
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:5500');
res.json(newMessages);
});
app.post('/messages', (req, res) => {
const newMessage = req.body;
messages.push(newMessage);
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:5500');
res.json({ message: 'Message added successfully' });
});
const server = app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});
server.setTimeout(5000);