-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
92 lines (65 loc) · 1.89 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const app = express();
mongoose.connect('mongodb://localhost:27017/myfirstmongodb', {useNewUrlParser: true,useUnifiedTopology:true});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
const Student = mongoose.model('Student', {
name: String,
student_id: Number,
email: String,
password: String,
date_added: Date
});
app.get('/', (req, res) => {
res.send('Welcome to my Node.js app');
});
app.get('/students', async (req, res) => {
const allStudents = await Student.find();
console.log('allStudents', allStudents);
res.send(allStudents);
});
app.post('/signup', async (req, res) => {
const body = req.body;
console.log('req.body', body)
const student = new Student(body);
const result = await student.save();
res.send({
message: 'Student signup successful'
});
});
app.post('/login', async (req, res) => {
const body = req.body;
console.log('req.body', body);
const email = body.email;
// lets check if email exists
const result = await Student.findOne({"email": email});
if(!result) // this means result is null
{
res.status(401).send({
Error: 'This user doesnot exists. Please signup first'
});
}
else{
// email did exist
// so lets match password
if(body.password === result.password){
// great, allow this user access
console.log('match');
res.send({message: 'Successfully Logged in'});
}
else{
console.log('password doesnot match');
res.status(401).send({message: 'Wrong email or Password'});
}
}
});
app.get('*', (req, res) => {
res.send('Page doesnot exists');
});
app.listen(3000, () => {
console.log('Express application running on localhost:3000');
});