-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
53 lines (41 loc) · 1.3 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
import express from "express";
import bodyParser from "body-parser";
import cors from "cors";
import mongoose from "mongoose";
import Promise from "bluebird";
import morgan from "morgan";
import passport from "passport";
import dotenv from "dotenv";
import routes from "./server/routes/index.route";
import passportConfig from "./server/config/passport";
const app = express();
dotenv.config();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Allow requests from any origin
app.use(cors({ origin: "*" }));
// log to console
app.use(morgan("dev"));
// Use the passport package in our application
app.use(passport.initialize());
/** plugin bluebird promise in mongoose */
mongoose.Promise = Promise;
/**
* Connection to db
*/
mongoose.connect(process.env.MONGODB_URL, { useNewUrlParser: true });
const db = mongoose.connection;
db.once("open", () => console.log("[✔️] MongoDB: Connection succeeded"));
db.on("error", () => {
throw new Error("[❌] MongoDB: Unable to connect");
});
/**
* Pass passport for configuration
*/
passportConfig(passport);
/** Mount all routes on /api/v1 path */
app.use("/api/v1", routes);
app.get("/", (req, res) => res.send("Welcome!"));
app.listen(process.env.PORT, function() {
console.log(`🚀 Server listening on port ${process.env.PORT}`);
});