This repository has been archived by the owner on Jul 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
74 lines (64 loc) · 1.49 KB
/
index.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
const dotenv = require('dotenv');
dotenv.config();
const config = require('./config');
const express = require('express');
/**
* Middlewares declarations
*/
// Handling cors
const cors = require('cors');
// Body parser for req/res
const bodyParser = require('body-parser');
// Nice cool logger (Bunyan)
const logger = require("./logger")();
// Express-session for maintaining sessions
const session = require('express-session');
// Redis client (Async using bluebird)
const client = require('./redis').getClient;
/**
* Other variables
*/
// Configure isProduction variable
const isProduction = config["env"] === 'production';
// Initialize app
const app = express();
/**
* Application Middlewares
*/
app.use(cors());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
app.use(session({
secret: 'express-session',
cookie: {
maxAge: 2000 * 3600 * 60 * 1000
},
resave: false,
saveUninitialized: false
}));
// ## Configure databases here.
require("./mongo");
/**
* Routes
*/
app.use("/", require("./routes/auth")({
client,
logger
}));
/**
* Error handlers & middlewares
*/
app.use((err, req, res, next) => {
logger.error(`[${req.method}][${req.url}] - ${JSON.stringify(err)}`);
res.status(err.status || 500).json({
errors: {
message: err.message,
error: isProduction ? {} : err,
},
});
});
app.listen(config["port"], () => {
console.log(`Server listening on port: ${config["port"]}`)
});