-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
79 lines (69 loc) · 2.13 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
const rateLimit = require('express-rate-limit');
const morgan = require('morgan');
const express = require('express');
const cors = require('cors');
const multer = require('multer');
const passport = require('passport');
const cookieParser = require('cookie-parser');
require('express-async-errors');
const upload = multer();
require('dotenv').config();
const app = express();
const connectDB = require('./config/db');
// initialize middleware
const loginLimiter = rateLimit({
windowMs: 60 * 60 * 1000, // 1 hour window
max: 20, // start blocking after 5 requests
statusCode: 200,
message: {
status: 429, // optional, of course
limiter: true,
type: 'error',
message: 'Too many login attemped from this IP, please try again after an hour',
},
});
const createLimiter = rateLimit({
windowMs: 60 * 60 * 1000, // 1 hour window
max: 2, // start blocking after 5 requests
statusCode: 200,
message: {
status: 429, // optional, of course
limiter: true,
type: 'error',
message: 'Too many accounts created from this IP, please try again after an hour',
},
});
// apply to all requests
app.use('/api/auth/login', loginLimiter);
app.use('/api/auth/register', createLimiter);
app.use(cookieParser());
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(morgan('dev'));
// initialize passport for OAuth2
app.use(passport.initialize());
app.use(passport.session());
require('./services/googleStrategy');
require('./services/facebookStrategy');
// for parsing multipart/form-data
app.use(upload.array());
app.use(express.static('public'));
// rutes for oAuth
app.use('/auth/google', require('./routes/googleAuth'));
app.use('/auth/facebook', require('./routes/facebookAuth'));
// general routes
app.use('/api/user', require('./routes/user'));
app.use('/api/auth', require('./routes/auth'));
// error handling
app.use((err, req, res, next) => {
// here will log the errors
res.status(500).send('Something failed.');
});
const port = process.env.PORT || 5000;
// app listener
app.listen(port, () => {
console.log(`Listening on port: ${port}`);
// connect to mongo DB
connectDB();
});