-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
48 lines (44 loc) · 1.19 KB
/
api.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
'use strict'
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const helmet = require('helmet');
const routes = require('./src/router.js');
const mongoose = require('mongoose');
mongoose.Promise = Promise;
mongoose.connect(`mongodb://${process.env.DB_USER}:${process.env.DB_PASS}@${process.env.DB_HOST}/${process.env.DB_NAME}`, {useNewUrlParser: true});
mongoose.connection.on('error', () => {
throw new Error(`unable to connect to database: ${process.env.DB_NAME}`);
});
const corsOptions = {
origin: (origin, callback) => {
if (!origin || process.env.CORS_WHITELIST.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback('Not allowed by CORS');
}
},
allowedHeaders: [
'Content-Type',
'Authorization',
'Content-Length',
'X-Requested-With',
'Accept',
],
methods: [
'GET',
'PUT',
'POST',
'DELETE',
'OPTIONS',
],
optionsSuccessStatus: 200,
};
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(helmet());
app.use(cors(corsOptions));
app.use('/', routes);
module.exports = app;