-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
46 lines (39 loc) · 1.25 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
// Get the packages we need
var express = require("express"),
router = express.Router(),
mongoose = require("mongoose"),
secrets = require("./config/secrets"),
bodyParser = require("body-parser");
// Create our Express application
var app = express();
// Use environment defined port or 4000
var port = process.env.PORT || 4000;
// Connect to a MongoDB --> Uncomment this once you have a connection string!!
mongoose.connect(secrets.mongo_connection, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
var db = mongoose.connection;
// Allow CORS so that backend and frontend could be put on different servers
var allowCrossDomain = function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept"
);
res.header("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS");
next();
};
app.use(allowCrossDomain);
// Use the body-parser package in our application
app.use(
bodyParser.urlencoded({
extended: true,
})
);
app.use(bodyParser.json());
// Use routes as a module (see index.js)
require("./routes")(app, router);
// Start the server
app.listen(port);
console.log("Server running on port " + port);