-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
42 lines (35 loc) · 1.08 KB
/
server.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
// Imports environment variables
require('./config/env')
// Loads database configuration
require('./config/database')
// Starts the app with express
const express = require('express')
const app = express()
// Allows handling multipart/form-data
const multer = require('multer')
const upload = multer()
// Imports routes
const router = require('./app/router')
app.set('port', process.env.PORT)
.set('useCreateIndex', true)
// For parsing JSON
.use(express.json())
// For parsing application/xwww-form-urlencode
.use(express.urlencoded({ extended: false }))
// For parsing multipart/form-data
.use(upload.array())
// Enables CORS
.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
res.header('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS')
next()
})
.use('/v1', router)
if (!module.parent) {
app.listen(
app.get('port'),
() => console.log(`Starting RESTful API... Listening on port: ${app.get('port')} `)
)
}
module.exports = app