-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
40 lines (39 loc) · 1.03 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
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const cors = require("cors");
const morgan = require("morgan");
const fs = require("fs");
const app = express();
const router = express.Router();
app.use(morgan("combined"));
app.use(bodyParser.json());
app.use(cors());
//connect to mongodb
mongoose
.connect(
"mongodb://localhost/movie_rating_app",
{ useNewUrlParser: true },
function() {
console.log("Connection has been made");
}
)
.catch(err => {
console.error("App starting error:", err.stack);
process.exit(1);
});
router.get("/", function(req, res) {
res.json({ message: "API Initialized!" });
});
// Include controllers
fs.readdirSync("controllers").forEach(function(file) {
if (file.substr(-3) == ".js") {
const route = require("./controllers/" + file);
route.controller(app);
}
});
const port = process.env.API_PORT || 8081;
app.use("/", router);
app.listen(port, function() {
console.log(`api running on port ${port}`);
});