-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
30 lines (25 loc) · 837 Bytes
/
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
const express = require('express');
const app = express();
const cors = require("cors");
app.use(express.json());
const whitelist = ["https://kayahan-todo-list-react.herokuapp.com"]
const corsOptions = {
origin: function (origin, callback) {
if (!origin || whitelist.indexOf(origin) !== -1) {
callback(null, true)
} else {
callback(new Error("Not allowed by CORS"))
}
},
credentials: true,
}
app.use(cors(corsOptions))
app.use('/', require('./routes/routes'));
//Global error handler
app.use((err, req, res, next) => {
console.log(err);
if(err && err.code) res.status(err.code).json({ message: err.message });
else res.status(500).json({ message: 'Opps! Something went wrong!'});
});
const port = process.env.PORT || 8080;
app.listen(port, () => console.log(`App running on port ${port}`));