-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
43 lines (36 loc) · 1.07 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
/*
contains all the application level config,
middlewares, and supporting libraries
*/
const express = require('express') // import express
const app = express() // initialize app with express
const bodyParser = require('body-parser')
const cookieParser = require('cookie-parser')
const logger = require('morgan')
// logger
app.use(logger('dev'))
// body parsers
app.use(bodyParser.json())
app.use(express.json())
app.use(cookieParser())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(express.urlencoded({ extended: true }))
// CORS
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*')
res.header(
'Access-Control-Allow-headers',
'Origin, X-Requested-With, Content-Type, Accept, Authorization'
)
if (req.method === 'OPTIONS') {
res.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH, DELETE, GET')
return res.status(200).json({})
}
next()
})
// import the routes
const routes = require('./app/routes/PostsRoutes')
// middleware to use the routes
app.use('/api', routes)
// export the app
module.exports = app