-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
59 lines (45 loc) · 1.72 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
require('dotenv').config()
const express = require('express');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
// Database
const MongoClient = require('mongodb').MongoClient;
let dbClient;
const mongoOptions = {
useNewUrlParser: true,
useUnifiedTopology: true,
reconnectInterval: process.env.MONGO_RECONNECTINTERVAL, // wait for 10 seconds before retry
reconnectTries: process.env.MONGO_RECONNECTTRIES, // retry forever
poolSize: process.env.MONGO_POOLSIZE,
}
// Initialize connection once
MongoClient.connect(process.env.MONGO_URI, mongoOptions)
.then(client => {
dbClient = client;
const db = client.db(process.env.MONGO_DB_NAME);
app.locals.db = db;
db.createCollection("students", { validator: { $jsonSchema: studentSchema } });
app.listen(process.env.API_PORT, () => console.info(`REST API running on port ${process.env.API_PORT}`));
}).catch(error => console.error(error));
// ROUTES
const studentsRouter = require ( `./api/${process.env.API_VERSION}/routes/student` );
const authRouter = require ( `./api/${process.env.API_VERSION}/routes/auth` );
// SCHEMAS
const studentSchema = require ( `./api/${process.env.API_VERSION}/schemas/student` );
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use('/', authRouter);
app.use('/students', studentsRouter);
// Application error handler
const errorHandler = require ( `./api/${process.env.API_VERSION}/middlewares/error` );
app.use ( errorHandler.app );
// Catch unhandled errors
app.use ( errorHandler.unhandled );
// listen for the signal interruption (ctrl-c)
process.on('SIGINT', () => {
dbClient.close();
process.exit();
});
module.exports = app;