-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
113 lines (98 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
require('dotenv').config()
const checkAuth = require('./checkauth')
const mongoose = require('mongoose')
const express = require("express");
const cors = require('cors')
const passport = require('passport')
const passportLocal = require('passport-local').Strategy
const cookieParser = require('cookie-parser')
const bcrypt = require('bcryptjs')
const session = require('express-session')
const bodyParser = require('body-parser')
const path = require("path");
const apiRoutes = require('./routes/api-routes');
const PORT = process.env.PORT || 3001;
const app = express();
const User = require('./models/user')
const morgan = require('morgan')
//code to allow commit
mongoose.connect(process.env.MONGODB_URI, {
// mongoose.connect("mongodb://localhost/powerrx", {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false
}).catch(err => {
console.log(err)
});
// Define middleware here
app.use(morgan("dev"))
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(cors({
origin: "http://localhost:3000", //location of the react app were connecting to
credentials: true
}))
const root = require('path').join(__dirname, 'client', 'build')
app.use(express.static(root));
app.use(session({
secret: 'secretcodoe',
resave: true,
saveUninitialized: true,
}))
// app.use(cookieParser('secretcode'));
// app.use(passport.initialize());
// app.use(passport.session());
// require('./config/passportConfig')(passport);
//Routes Passport
// app.post('/login',(req,res, next) => {
// passport.authenticate('local', (err, user,info) => {
// if (err) throw err
// if (!user) res.send('No User Exists')
// else {
// req.login(user, err => {
// if (err) throw err
// res.send('Successfully Authenticated')
// console.log(req.user)
// })
// }
// })(req,res,next)
// })
// app.post('/register',(req,res) => {
// User.findOne({username: req.body.username}, async (err,doc) => {
// if (err) throw err;
// if (doc) res.send('User Already Exists');
// if (!doc) {
// async function asyncCall() {
// const hashedPassword = await bcrypt.hash(req.body.password, 10)
// const newUser = new User({
// username:req.body.username,
// password: hashedPassword
// });
// await newUser.save();
// res.send('User Created')
// }
// asyncCall()
// }
// })
// })
// app.get('/User',(req,res) => {
// res.send(req.user);
// })
//Starter code
// app.use(express.urlencoded({ extended: true }));
// app.use(express.json());
// Serve up static assets (usually on heroku)
// if (process.env.NODE_ENV === "production") {
// app.use(express.static("client/build"));
// }
// Define API routes here
app.use("/api", checkAuth, apiRoutes )
// Send every other request to the React app
// Define any API routes before this runs
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "./client/build/index.html"));
});
app.listen(PORT, () => {
console.log(`🌎 ==> API server now on port ${PORT}!`);
});