generated from BuildForSDG/js-starter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
49 lines (38 loc) · 1.11 KB
/
app.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
// export default appName;
require("dotenv").config();
const { errors } = require("celebrate");
const express = require("express");
const mongoose = require("mongoose");
const cors = require("cors");
const passport = require("passport");
const logger = require("morgan");
// Setting up port
const connUri = process.env.MONGO_URI;
//= == 1 - CREATE APP
// Creating express app and configuring middleware needed for authentication
const app = express();
app.use(logger("dev"));
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// form-urlencoded
//= == 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.on("error", () => {
process.exit();
});
//= == 3 - INITIALIZE PASSPORT MIDDLEWARE
app.use(passport.initialize());
require("./middlewares/jwt")(passport);
//= == 4 - CONFIGURE ROUTES
// Configure Route
require("./routes/index")(app);
app.use(errors());
module.exports = app;