-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
39 lines (31 loc) · 1.29 KB
/
Copy pathserver.js
File metadata and controls
39 lines (31 loc) · 1.29 KB
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
const express = require("express");
const mongoose = require("mongoose");
const cors = require("cors");
require('dotenv').config({path: "./config/keys.env"});
const customerController = require("./controllers/customerController.js");
const propertyController = require("./controllers/propertyController.js");
const app = express();
const allowlist = ['http://localhost:3000', 'http://127.0.0.1:3000', 'https://priceless-mirzakhani-0f2db6.netlify.app'];
const corsOptionsDelegate = function (req, callback) {
var corsOptions;
if (allowlist.indexOf(req.header('Origin')) !== -1) {
corsOptions = { origin: true } // reflect (enable) the requested origin in the CORS response
} else {
corsOptions = { origin: false } // disable CORS for this request
}
callback(null, corsOptions); // callback expects two parameters: error and options
}
app.use(cors(corsOptionsDelegate));
app.use(express.json());
app.use("/customers", customerController);
app.use("/properties", propertyController);
app.listen(process.env.PORT, ()=>{
console.log(`The REST API is running on PORT: ${process.env.PORT}`);
mongoose.connect(process.env.MONGODB_QUERY_STRING)
.then(()=>{
console.log(`Connected to MongoDB`);
})
.catch(err=>{
console.log(`Error: ${err}`);
});
});