This repository has been archived by the owner on Sep 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
114 lines (92 loc) · 2.38 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
const express = require('express');
const bodyParser = require('body-parser');
const bcrypt = require('bcrypt-nodejs');
var cors = require('cors')
const app = express();
app.use(cors());
app.use(bodyParser.json());
const database = {
users: [
{
id: '123',
firstname: 'john',
lastname: 'talavi',
email: '[email protected]',
password: 'cookies',
entries: 0,
joined: new Date()
},
{
id: '124',
firstname: 'sally',
lastname: 'bonner',
email: '[email protected]',
password: 'bananas',
entries: 0,
joined: new Date()
}
],
Login:[{
id: '987',
hash: ' ',
email: '[email protected]'
}]
}
app.get('/', (req,res) => {
res.send('This is working');
});
app.get('/profile/:id',(req,res)=>{
const { id } = req.params;
database.users.forEach(user => {
if (user.id === id)
return res.json(user);
});
return res.status(404).json('no such user');
});
app.post('/signin',(req,res)=>{
if (req.body.email === database.users[0].email &&
req.body.password === database.users[0].password)
{
res.json('success');
} else{
res.status(400).json('error logging in');
}
});
app.post('/register',(req,res)=>{
const {email, password, firstname, lastname} = req.body;
database.users.push({
id: '125',
firstname: firstname,
lastname: lastname,
email: email,
password: password,
entries: '',
joined: new Date()
})
res.json(database.users[database.users.length-1]);
});
app.put('/image', (req,res)=>{
const { id } = req.body;
database.users.forEach(user => {
if (user.id === id){
user.entries++;
return res.json(user.entries);
}
});
return res.status(404).json('Invalid Request');
})
/*
bcrypt.hash("bacon", null, null, function(err, hash) {
// Store hash in your password DB.
});
// Load hash from your password DB.
bcrypt.compare("bacon", hash, function(err, res) {
// res == true
});
bcrypt.compare("veggies", hash, function(err, res) {
// res = false
});
*/
app.listen(3000, () => {
console.log("App is running on port 3000")
});