-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
36 lines (29 loc) · 1006 Bytes
/
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
const express = require('express')
const cors = require('cors')
const route = require('./routes/route')
const HttpError = require('./model/http-error')
const app = express()
const PORT = process.env.PORT || 5000
//CORS policy protection
const whitelist = ['https://covidzones.prasoon.me', 'https://covid-zones.web.app']
const corsOptions = {
origin: whitelist,
optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
}
app.use(cors(corsOptions))
app.use(express.json({extended: false}))
app.use('/',route)
//error handling for unknown routes
app.use((req, res, next) => {
const error = new HttpError('Could not find this route.', 404);
return next(error)
})
//error handling
app.use((error, req, res, next) => {
if (res.headerSent) {
return next(error)
}
res.status(error.code)
res.json({message: error.message || 'Something went wrong, server error'})
})
app.listen(PORT,() => console.log(`Server started on PORT ${PORT} ..`))