-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
116 lines (98 loc) · 3.01 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
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
const express = require('express');
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const app = express();
const port = 3000;
//inmemory
const users =[];
app.set('view engine', 'ejs');
app.use(express.static('public'))
app.use(express.json())
app.use(express.urlencoded({ extended: true }));
var moment = require('moment');
mongoose
.connect('mongodb+srv://rookie:[email protected]/test', {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});
})
.catch((err) => {
console.error('MongoDB connection error:', err.message);
});
// auto refresh
const path = require("path");
const livereload = require("livereload");
const liveReloadServer = livereload.createServer();
liveReloadServer.watch(path.join(__dirname, 'public'));
const connectLivereload = require("connect-livereload");
app.use(connectLivereload());
liveReloadServer.server.once("connection", () => {
setTimeout(() => {
liveReloadServer.refresh("/");
}, 100);
});
// Models
const Users = require("./models/usersSchema")
// Get request
app.get('/', (req, res) => {
res.render('index', { req: req });
});
app.get('/users', (req, res) => {
Users.find().then((allusers) => {
console.log(allusers);
res.render('users/users', { Array: allusers, moment: moment, req: req });
}).catch((error) => {
console.log(error)
});
});
// Post request
app.post('/users/add', (req, res) => {
console.log(req.body)
Users.create(req.body)
.then(result => {
res.redirect("/users");
})
.catch(err => {
console.log(err);
});
});
app.post('/register', async (req, res) => {
try {
const {email, password} = req.body;
// Find User
const findUser = users.find((data)=> email == data.email);
if (findUser) {
res.status(400).send("Wrong Email or password!");
}
// Hash the password
const hashedPassword = await bcrypt.hash(password, 10);
users.push({email , password: hashedPassword});
console.log(users)
res.status(201).send("Registered successfully!");
} catch (err) {
res.status(500).send({ message: err.message });
}
});
app.post('/login', async (req, res) => {
try {
const {email, password} = req.body;
// Find User
const findUser = users.find((data)=> email == data.email);
if (!findUser) {
res.status(400).send("Wrong Email or password!");
}
// Password Match
const passwordMatch = await bcrypt.compare(password, findUser.password);
if(passwordMatch){
res.status(200).send("Logged in successfully!")
} else {
res.status(400).send("Wrong Email or password!");
}
} catch (err) {
res.status(500).send({ message: err.message });
}
});