-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
55 lines (47 loc) · 1.34 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
// Load environment vars and DB
require('dotenv').config()
require('./config/db')
// Import packages
const express = require('express')
const path = require('path')
const bodyParser = require('body-parser')
const busboyBodyParser = require('./helpers/body-parser')
const logger = require('morgan')
const cors = require('cors')
const validator = require('express-validator')
const app = express()
const server = require('./config/server')
const versions = ['v1']
require('./config/template').configure(app)
global._base = path.join(__dirname, '/')
// Middlewares
if (app.get('env') === 'development') {
app.use(logger('dev'))
app.get('/template', (req, res) => {
res.render(`${req.query.path}`)
})
}
app.use(busboyBodyParser())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cors({
exposedHeaders: ['Content-Disposition']
}))
app.use(validator())
app.use(express.static('public'))
// Set global response headers
app.use((req, res, next) => {
res.setHeader('Content-Type', 'application/json')
next()
})
// Apply the routes for each version setted
versions.forEach((version) => {
const versionRoutes = require('./modules/' + version + '/routes')
app.use('/api/' + version, versionRoutes)
})
app.get('/', (req, res) => {
res.status(200).json({})
})
// Start server
server.start(app)
module.exports = app