forked from rohitdwivedula/mern-crud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (57 loc) · 2.16 KB
/
index.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
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const mongoose = require("mongoose");
const passport = require("passport")
const path = require("path")
const cookieSession = require("cookie-session");
require("dotenv").config()
var app = express(); //This is our server object
PORT = process.env.PORT || 5050
/*
Middleware section: Here we specify our middleware, middlewares are basically functions that modify
request and response objects and some processing before they are handled by our routes
- body-parser provides middleware to modify the data sent as part of the requests to that
is appears/oraganized and easily accessed
*/
app.use(cors({origin: true, credentials: true}));
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, "client", "build")))
var device = require('express-device');
app.use(device.capture());
/**
The below lines import the schema that we created for storing movies, and then sets some
default values for mongoose to prevent too many warnings from popping up. The mongoose.connect()
statement in the code connects your application with MongoDB.
**/
require("./models/User.js");
mongoose.set('useCreateIndex', true);
mongoose.set('useUnifiedTopology', true);
mongoose.set('useFindAndModify', false);
DATABASE_CONNECTION = process.env.MONGODB_URI || "mongodb://localhost:27017/movies-crud"
console.log(DATABASE_CONNECTION)
mongoose.connect(DATABASE_CONNECTION,{useNewUrlParser: true});
app.use(
cookieSession({
maxAge: 30 * 24 * 60 * 60 * 1000,
keys: ["somesecretsauce"]
})
);
/**
PassportJS - JWT Authentication With Google
**/
app.use(passport.initialize());
app.use(passport.session());
require("./passport.js");
require("./routes/api/auth.js")(app);
require("./routes/api/movie.js")(app);
require("./routes/api/stats.js")(app);
/// API Endpoints Begin Here
app.get("/api", (req,res)=>{
res.status(200).send({"message": "Welcome to the movie microservice API."});
});
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "client", "build", "index.html"));
});
module.exports = app