generated from BloomTech-Labs/template-be
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
54 lines (47 loc) · 1.26 KB
/
server.js
File metadata and controls
54 lines (47 loc) · 1.26 KB
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
const express = require('express');
const graphqlHTTP = require('express-graphql');
const morgan = require('morgan');
const cors = require('cors');
const helmet = require('helmet');
const schema = require('./schema/schema');
const bodyParser = require('body-parser');
const passport = require('passport');
const authRoutes = require('./routes/auth-routes');
const socialAuthRoutes = require('./routes/social-auth-routes');
const rateLimit = require('express-rate-limit');
const app = express();
// Middleware
app.use(cors());
app.use(helmet());
app.use(morgan('combined'));
app.use(
bodyParser.urlencoded({
extended: false,
}),
);
app.use(bodyParser.json());
// Passport initialize
app.use(passport.initialize());
require('./config/passport-config.js')(passport);
// Rate Limit
const authLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100,
});
const backendLimiter = rateLimit({
windowMs: 60 * 60 * 1000, // 60 minutes
max: 1000,
message: 'Too many attempts, please try again after an hour',
});
// routes
app.use('/auth', authLimiter, [authRoutes, socialAuthRoutes]);
app.use(
'/backend',
backendLimiter,
passport.authenticate('jwt', { session: false }),
graphqlHTTP({
schema,
graphiql: true,
}),
);
module.exports = app;