-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
111 lines (95 loc) · 3.39 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
const express = require('express');
const app = express();
const port = process.env.PORT || 5000;
var mongoose = require("mongoose");
var bodyParser = require('body-parser');
const morgan = require('morgan');
const session = require('express-session');
const dbConnection = require('./client/src/config/userDataConnection');
const MongoStore = require('connect-mongo')(session);
const passport = require('./client/src/passport');
//const User = require('./client/src/routes/user');
const User = require('./client/src/models/user');
// Require all models
var Sensor = require("./client/src/models/sensorModel");
// Sessions
app.use(
session({
secret: 'projectsix', //pick a random string to make the hash that is generated secure
store: new MongoStore({ mongooseConnection: dbConnection }),
resave: false, //required
saveUninitialized: false //required
})
)
// Passport
app.use(passport.initialize());
app.use(passport.session()); // calls serializeUser and deserializeUser
app.use(morgan('dev'))
// body parser things
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json())
//connect to mongo
mongoose.connect("mongodb+srv://Rebecca:[email protected]/test?retryWrites=true&w=majority", { useNewUrlParser: true });
// console.log that your server is up and running
app.listen(port, () => console.log(`Listening on port ${port}`));
// create a GET route
app.get('/express_backend', (req, res) => {
//res.send({ express: 'YOUR EXPRESS BACKEND IS CONNECTED TO REACT' });
Sensor.find().then(found => {
//console.log(found);
res.send({express: found});
});
});
app.use( (req, res, next) => {
console.log('req.session', req.session);
return next();
});
/*---- ROUTE TO CREATE NEW SENSOR PRODUCT ----*/
app.post('/new_sensor', (req, res) => {
// Create a new mongo sensor entry using data
Sensor.findOneAndUpdate({product:req.body.item}, {reading:100, reorderLink:req.body.link},{upsert:true})
.then(function(sensorReading) {
// If saved successfully, send the the new User document to the client
console.log(sensorReading);
})
.catch(function(err) {
// If an error occurs, send the error to the client
console.log(err);
});
})
// app.post('/signup', (req, res) => {
// // Create a new mongo sensor entry using data
// User.create({username:req.body.username, password:req.body.password})
// .then(function(newUser) {
// // If saved successfully, send the the new User document to the client
// console.log(newUser);
// })
// .catch(function(err) {
// // If an error occurs, send the error to the client
// console.log(err);
// });
// })
app.post('/signup', (req, res) => {
console.log('user signup');
const { username, password } = req.body
// ADD VALIDATION
User.findOne({ username: username }, (err, user) => {
if (err) {
console.log('User.js post error: ', err)
} else if (user) {
res.json({
error: `Sorry, already a user with the username: ${username}`
})
}
else {
const newUser = new User({
username: username,
password: password
})
newUser.save((err, savedUser) => {
if (err) return res.json(err)
res.json(savedUser)
})
}
})
})