-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (33 loc) · 1.01 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
'use strict'
const express=require('express'),
morgan=require('morgan'),
mongoose=require('mongoose'),
app=express(),
cors=require('cors')
const config=require('./config')
const api=require('./routes')
mongoose.connect(config.db,{useNewUrlParser: true},function(){})
.then(()=>{console.log('Connected to Database')})
.catch(err=>{console.log(`Not Connected to Database ERROR!, ${err}`)})
app.use(cors())
app.use(morgan('dev'))
app.use(express.json())
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()
})
app.use('/api', api)
app.get('/', (req,res)=>{
res.send('Hello World!')
})
app.listen(config.port, ()=>{
console.log(`Server running in port: ${config.port}`)
})