This repository has been archived by the owner on Dec 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
89 lines (78 loc) · 1.99 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* This is an attempt of me creating a pintress clone
*/
const express = require("express");
const session = require("express-session");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const morgan = require("morgan");
const config = require("./config/config");
//Middleware Protection
const passport = require("passport");
// Passport Config
require("./services/passport")(passport);
//Routes
const router = require("./routes/routes");
//Create the app on express
const app = express();
//DB Config
// const db_key = require('./config/config').mongoURI;
//Connect to MongoDB
mongoose
.connect(
config.mongoURI, {
useNewUrlParser: true
}
)
.then(() => {
console.log("Mongo DB Connected!");
})
.catch(err => {
console.error(err);
});
//Body parser middleware. This middle allows to do req.body
app.use(
bodyParser.urlencoded({
extended: false
})
);
//Parse in json format
app.use(bodyParser.json());
//Log the api requests temporary
app.use(morgan("dev"));
//Protecting our routes, we initialize the passport middleware
app.use(
require("express-session")({
secret: "keyboard cat",
resave: true,
saveUninitialized: true
})
);
// app.use(passport.initialize());
// app.use(passport.session());
// CORS Support
// app.use(CORS);
// //Basic route test
// app.get("/", (req, res, next) => {
// var html =
// "<ul>\
// <li><a href='/api/user/auth/github'>GitHub</a></li>\
// <li><a href='/api/user/logout'>logout</a></li>\
// </ul>";
// res.send(html);
// });
// app.get("/logout", function(req, res) {
// console.log("logging out");
// req.logout();
// res.redirect("/");
// });
//Use API Routes
app.use("/api", router);
let port = process.env.PORT || 5000;
//If C9 ide
if (port == 8080) {
//Set the port to 8081
port = 8081;
}
const host = process.env.HOST || process.env.IP || "localhost";
app.listen(port, () => console.log(`Server running at http://${host}:${port}`));