forked from davidornelas11/MERN-location-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
58 lines (50 loc) · 1.62 KB
/
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
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
const express = require("express");
const mongoose = require("mongoose");
const connection =
"mongodb+srv://davidornelas:Project3^^^@cluster0.ypzc4.mongodb.net/mern-location-api?retryWrites=true&w=majority";
const PORT = process.env.PORT || 3001;
const app = express();
const MONGODB_URI =
process.env.MONGODB_URI || "mongodb://localhost:27017" + "MERN-location-app";
const cors = require("cors");
app.use(express.urlencoded({ extended: false }));
//middleware
app.use(express.json());
const whitelist = [
"http://localhost:3000",
"http://localhost:3001",
"https://destinations-api-project3.herokuapp.com/users",
"https://destinations-api-project3.herokuapp.com/locations",
"https://destinations-api-project3.herokuapp.com",
];
const corsOptions = {
origin: function (origin, callback) {
if (whitelist.includes(origin)) {
callback(null, true);
} else {
callback(new Error("Not allowed by CORS"));
}
},
};
app.use(cors(corsOptions));
// Database Error / Disconnection
mongoose
.connect(connection, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
})
.then(() => console.log("Database Connected Successfully"))
.catch((err) => console.log(err));
// Database Connection Successful
mongoose.connection.once("open", () => {
console.log("connected to mongoose!");
});
//Controllers and Routes
const locationsController = require("./controllers/locations");
app.use("/locations", locationsController);
const usersController = require("./controllers/users");
app.use("/users", usersController);
app.listen(PORT, () => {
console.log("listening on port" + PORT);
});