-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
61 lines (49 loc) · 1.56 KB
/
Copy pathserver.js
File metadata and controls
61 lines (49 loc) · 1.56 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require("dotenv").config();
const express = require("express");
const mongoose = require("mongoose");
const cors = require("cors");
const passport = require("passport");
const path = require("path");
// Setting up port
const connUri = process.env.MONGO_LOCAL_CONN_URL;
let PORT = process.env.PORT || 3000;
//=== 1 - CREATE APP
// Creating express app and configuring middleware needed for authentication
const app = express();
app.use(cors());
// for parsing application/json
app.use(express.json());
// for parsing application/xwww-
app.use(express.urlencoded({ extended: false }));
//form-urlencoded
// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "jade");
//=== 2 - SET UP DATABASE
//Configure mongoose's promise to global promise
mongoose.promise = global.Promise;
mongoose.connect(connUri, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
});
const connection = mongoose.connection;
connection.once("open", () =>
console.log("MongoDB -- database connection established successfully!")
);
connection.on("error", (err) => {
console.log(
"MongoDB connection error. Please make sure MongoDB is running. " + err
);
process.exit();
});
//=== 3 - INITIALIZE PASSPORT MIDDLEWARE
app.use(passport.initialize());
require("./middlewares/jwt")(passport);
//=== 4 - CONFIGURE ROUTES
//Configure Route
require("./routes/index")(app);
//=== 5 - START SERVER
app.listen(PORT, () =>
console.log("Server running on http://localhost:" + PORT + "/")
);