forked from fawwaz37/nodejs-restapi
-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
86 lines (70 loc) · 2.16 KB
/
app.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
const express = require('express');
const app = express();
const session = require('express-session');
const cookieParser = require('cookie-parser');
const expressLayout = require('express-ejs-layouts');
const rateLimit = require("express-rate-limit");
const passport = require('passport');
const flash = require('connect-flash');
const MemoryStore = require('memorystore')(session);
const apiRouters = require('./routes/api');
const userRouters = require('./routes/users');
const { isAuthenticated } = require('./lib/auth');
const { connectMongoDb } = require('./database/connect');
const { getApikey } = require('./database/db');
const { port } = require('./lib/settings');
const PORT = process.env.PORT || port;
connectMongoDb();
app.set('trust proxy', 1);
const limiter = rateLimit({
windowMs: 1 * 60 * 1000,
max: 50,
message: 'Oops too many requests'
});
app.use(limiter);
app.set('view engine', 'ejs');
app.use(expressLayout);
app.use(express.static('public'));
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: true,
cookie: { maxAge: 86400000 },
store: new MemoryStore({
checkPeriod: 86400000
}),
}));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cookieParser());
app.use(passport.initialize());
app.use(passport.session());
require('./lib/config')(passport);
app.use(flash());
app.use(function(req, res, next) {
res.locals.success_msg = req.flash('success_msg');
res.locals.error_msg = req.flash('error_msg');
res.locals.error = req.flash('error');
res.locals.user = req.user || null;
next();
})
app.get('/', (req, res) => {
res.render('index', {
layout: 'layouts/main'
});
});
app.get('/docs', isAuthenticated, async (req, res) => {
let getkey = await getApikey(req.user.id)
let { apikey, username } = getkey
res.render('docs', {
username: username,
apikey: apikey,
layout: 'layouts/main'
});
});
app.use('/api', apiRouters);
app.use('/users', userRouters);
app.set('json spaces', 4);
app.listen(PORT, () => {
console.log(`App listening at http://localhost:${PORT}`);
});