-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (32 loc) · 890 Bytes
/
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
const express = require("express");
const cors = require("cors");
const path = require("path");
require('dotenv').config()
const routes = require("./routes/index")
const whitelist = [process.env.FRONTEND]
const corsOptions = {
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1) {
callback(null, true)
} else {
// no access
callback(null, false)
}
}
}
const app = express();
app.use(cors(corsOptions))
app.use(express.json())
app.use(express.urlencoded({extended: true}))
// subscribes all routes
app.use(routes)
app.get("*", (req, res)=>{
let html = path.resolve("index.html")
res.sendFile(html)
})
// global error handling middleware
app.use((err, req, res, next) => {
res.status(500).send(err.message || 'Something broke!')
})
const PORT = process.env.PORT || 4000
app.listen(PORT, ()=>console.log(`Server id running port ${PORT}`))