-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
106 lines (86 loc) · 2.94 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
require('module-alias/register')
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const cors = require('cors');
const morgan = require('morgan');
const helmet = require('helmet');
const dotenv = require('dotenv');
const bodyParser = require('body-parser');
const path = require('path');
const RateLimit = require('express-rate-limit');
dotenv.config();
const { PORT, mongoUri } = require('./config');
const authRoute = require('./routes/api/auth')
const usersRoute = require('./routes/api/users')
const postsRoute = require('./routes/api/posts')
// const routeAuth = require('@auth/routes');
// const routeUsers = require('@users/routes');
// const routePosts = require('@posts/routes');
// set up rate limiter: maximum of five requests per minute
const limiter = new RateLimit({
windowMs: 1 * 60 * 1000, // 1 minute
max: 30
});
app
.use(cors())
.use(morgan('tiny'))
.use(bodyParser.json())
.use(limiter)
.use(helmet())
//csp
app.use(function (req, res, next) {
res.setHeader(
'Content-Security-Policy', "script-src 'self'; img-src 'self' https://i.imgur.com https://w7.pngwing.com/; frame-src 'self'"
);
next();
});
mongoose
.connect(mongoUri)
.then(() => {
console.log('Connection to MongoDB is successful.');
})
.catch((err) => console.log(err));
//api
app.use('/api/auth', authRoute);
app.use('/api/users', usersRoute);
app.use('/api/posts', postsRoute);
// app.use('/api/v1/auth', routeAuth);
// app.use('/api/v1/users', routeUsers);
// app.use('/api/v1/posts', routePosts);
//get front
if (process.env.NODE_ENV == 'production') {
app.use(express.static('client/dist'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'dist', 'index.html'));
})
}
app.listen(PORT, () => console.log((`Node listening at http://localhost:${PORT}`)));
//sockets
const http = require('http');
const httpServer = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(httpServer, {
cors: {
origin: "https://localhost:3030",
methods: ["GET", "POST"]
}
});
io.on('connection', (socket) => {
socket.on('online', (data) => {
console.log('Message received :', data.username);
socket.broadcast.emit('onlinenotif', { data: data.username });
socket.join(data.username);
});
socket.on('likedpost', (data) => {
console.log(`${data.from.username} liked your post: ${data.post.content} (${data.post.author.username})`);
socket.broadcast.to(data.post.author.username).emit('likednotif', {from: data.from, post: data.post});
});
socket.on('followeduser', (data) => {
console.log('Message received :', data.from.username + ' followed you!');
// socket.broadcast.to(data.user.username).emit('followednotif', {data: data.user});
});
});
httpServer.listen(3030, () => {
console.log('Sockets listening on *:3030');
});