This repository has been archived by the owner on Mar 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
76 lines (70 loc) · 1.98 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
require('dotenv').config();
const express = require('express');
const logger = require('morgan');
const cors = require('cors');
const session = require('express-session');
const createError = require('http-errors');
const SequelizeStore = require('connect-session-sequelize')(session.Store);
const routes = require('./api/v1/routes/routes');
const errHandler = require('./api/v1/middleware/errHandler');
const db = require('./api/v1/db');
const database = require('./api/v1/db/db');
const Sequelize = require('./api/v1/db/index');
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
const Session = database.define('Session', {
sid: {
type: Sequelize.STRING,
primaryKey: true,
},
userId: Sequelize.STRING,
expires: Sequelize.DATE,
data: Sequelize.STRING(50000),
});
const sessionStore = new SequelizeStore({
db: database,
table: 'Session',
checkExpirationInterval: 15 * 60 * 1000,
expiration: 10800000,
});
app.use(logger('dev'));
app.use(cors({ origin: true, credentials: true }));
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true,
store: sessionStore,
cookie: { maxAge: 10800000 },
})
);
app.use(routes);
app.use(errHandler.errHandler);
/* disabled for development
It interferes with err handling
app.use('/', function (req, res) {
return res.status(200).json({
status: 200,
Message: 'Welcome To covid API Kenya',
});
});
*/
// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404));
});
// Invalid status code error
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function (err, req, res, next) {
res.status(err.status || 500).json({
status: err.status,
message: err.message,
});
});
}
sessionStore.sync();
Session.sync({ alter: true });
module.exports = app;