-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
73 lines (51 loc) · 1.82 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//Dependencies for the server
/* const result = require('dotenv').config()
if (result.error) {
throw result.error
} */
const express = require("express");
const mongoose = require("mongoose");
const cors = require("cors");
const path = require("path");
const body_parser = require("body-parser");
const passport = require("passport");
const keys = require("./config/keys");
const compression = require("compression");
//Conects to the mongoDb database
mongoose.connect(keys.mongodb.dbURI,{ useNewUrlParser: true ,useUnifiedTopology: true } );
//Checks for database connection
mongoose.connection.on("connected", function () {
console.log("The server has connected to the database");
})
//Checks for database connection error
mongoose.connection.on("error", function (err) {
console.log("The server has failed to connect to the database: " + err);
})
//Creates application with framework(routing is main)
const app = express();
//Used for all /api requests
const api = require("./routes/api");
//Port for the server
const port = process.env.PORT || 80;
//Uses CORS middleware
app.use(cors());
//Use body parser for parsing application/json
app.use(body_parser.json());
// Passport middleware for token authentication
app.use(passport.initialize());
app.use(passport.session());
// install it like expressJS middleware
app.use(compression());
require('dotenv').config()
require("./config/passport")(passport);
//Static folder for Client part
app.use(express.static(path.join(__dirname, "/public/")));
//Router for the /users api
app.use("/api", api);
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "/public/index.html"));
})
//Starts the server
app.listen(port, function () {
console.log("The server has started at port " + port);
});