-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
69 lines (56 loc) · 1.73 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
const express = require("express");
const mongoose = require("mongoose");
const cookieParser = require("cookie-parser");
const session = require("express-session");
const passport = require("passport");
const bodyParser = require("body-parser");
require("./database/models/user_model");
require("./database/models/listing_model");
require("./config/passport")(passport);
//// Loading routes ////
const auth = require("./routes/auth_routes");
const listing = require("./routes/listing_routes");
const user = require("./routes/user_routes");
//// Loading mongoose keys ////
const keys = require("./config/keys");
mongoose.Promise = global.Promise;
// mongoose connect //
mongoose
.connect(
keys.mongoURI,
{
useNewUrlParser: true
}
)
.then(() => console.log("MongoDb connection"))
.catch(err => console.log("mongodb error is " + err));
const app = express();
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(
session({
secret: "secret",
resave: false,
saveUninitialized: false
})
);
//// passport initialize ////
app.use(passport.initialize());
app.use(passport.session());
// routes //
app.use("/auth", auth);
app.use("/api/listing", passport.authenticate('jwt', { session: false }), listing);
app.use("/api/users", passport.authenticate('jwt', { session: false }), user);
// for production build //
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
const path = require("path");
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
var port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`started server on port ${port}`);
});