-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
121 lines (91 loc) · 3.08 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const express = require("express");
require("dotenv").config();
const session = require("express-session");
const passport = require('passport');
const passportlocal= require("passport-local").Strategy;
const bodyParser = require('body-parser');
const User = require("./models/user.js")
const path = require("path");
const PORT = process.env.PORT || 3001;
const app = express();
const mongoose = require("mongoose");
const db = require("./models");
const cors = require("cors");
console.log(process.env.PORT, "this is the port")
// Define middleware here
app.use(express.urlencoded({ extended: true }));
// Body parser
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: false }));
// CORS
app.use(cors({origin: process.env.origin_url}))
// Serve up static assets (usually on heroku)
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
}
// app.use(session({ secret: 'keyboard cat', resave: true, saveUninitialized: true }));
// app.use(passport.initialize());
// app.use(passport.session());
// mongoose.connect('mongodb://127.0.0.1:27017/passport-jwt', { useMongoClient: true });
/* mongoose.connect('mongodb://127.0.0.1:27017/comedyshows', {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
// useFindAndModify: false
});
// mongoose.set("useCreateIndex", true); */
mongoose.connection.on('error', error => console.log(error) );
mongoose.Promise = global.Promise;
// Proj database
mongoose.connect(process.env.MONGODB_URI || "mongodb://localhost/comedyshows",
{
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false
});
require("./auth/passport.js")
const routes = require('./routes');
// const secureRoute = require('./routes/secure_routes/secure-routes');
app.use('/', routes);
// Plug in the JWT strategy as a middleware so only verified users can access this route.
// app.use('/user', passport.authenticate('jwt', { session: false }), secureRoute);
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.json({ error: err });
});
// Define API routes here
// app.use(routes);
// Send every other request to the React app
// Define any API routes before this runs
// app.get("/", (req, res) => {
// res.send("Hello world!");
// });
// app.post("/login", (req, res) => {
// // console.log(req.body)
// db.User.findOne({email: req.body.email})
// // db.User.find()
// // db.User.create(req.body)
// .then(dbUser => {
// if (req.body.password === dbUser.password) {
// res.send("Password Correct");
// }else{
// res.send("Incorrect Password");
// }
// // console.log(dbUser);
// // res.json(dbUser);
// })
// .catch(err => {
// res.json(err);
// });
// // res.send("Login!");
// });
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "./client/build/index.html"));
});
// app.get("*", (req, res) => {
// res.sendFile(path.join(__dirname, "./client/build/index.html"));
// });
app.listen(PORT, () => {
console.log(`🌎 ==> API server now on port ${PORT}!`);
});