-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (53 loc) · 1.79 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const express = require ('express')
const cors = require ('cors')
const db = require('./src/db')
const app = express()
const apiPort = 3603
const fileUpload = require('express-fileupload');
const routes =require('./src/routes/species-routes')
app.use(express.urlencoded({extended:true}))
app.use(cors("*"))
app.use(express.json())
app.use(fileUpload({
createParentPath: true
}));
// app.use(require("./routes/species-routes"));
db.on('error', console.error.bind(console, 'MongoDB connection error:'))
// app.get('/', (req,res)=>{
// res.send('Hello World')
// })
const accessControl = (req, res,next) => {
const allowedOrigins = [
'http://127.0.0.1:3603', 'http://localhost:3603','http://localhost:3603', 'https://kaabil.net'
];
const origin = req.headers.origin;
/*
if (origin && typeof origin === 'string' && allowedOrigins.indexOf(origin) > -1) {
res.setHeader('Access-Control-Allow-Origin', origin);
}
*/
res.header('Access-Control-Allow-Origin', 'https://kaabil.net');
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, KBL-User-Agent');
res.header('Access-Control-Allow-Credentials', 'true');
return next();
}
// Allows other domains to use this domain as an API
const originsWhitelist = [
'http://127.0.0.1:3603', 'http://localhost:3603', 'http://localhost:3603'
];
const corsOptions = {
origin: (origin, callback) => {
if (origin && originsWhitelist.indexOf(origin) >= -1) {
return callback(null, true);
}
const error = new Error('CORS Error');
return callback(error, false);
}
}
const cOpt = {
origin: 'http://localhost:3603',
credentials: true
}
app.use("/api", routes)
app.listen(apiPort, ()=> console.log(`Server runnning on port ${apiPort}`))