-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
54 lines (46 loc) · 1.33 KB
/
server.js
File metadata and controls
54 lines (46 loc) · 1.33 KB
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
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.json())
const mockUserData = [
{ name: 'Mark' },
{ name: 'Jill' }
]
app.get('/users', function (req, res) {
res.json({
success: true,
message: 'successfully got users. Nice!',
users: mockUserData
})
})
// colons are used as variables that be viewed in the params
app.get('/users/:id', function (req, res) {
console.log(req.params.id)
res.json({
success: true,
message: 'got one user',
user: req.params.id
})
})
app.post('/login', function (req, res) {
// Typically passwords are encrypted using something like bcrypt before sending to database
const username = req.body.username;
const password = req.body.password;
// This should come from the database
const mockUsername = "billyTheKid";
const mockPassword = "superSecret";
if (username === mockUsername && password === mockPassword) {
// In practice, use JSON web token sign method here to make an encrypted token
res.json({
success: true,
message: 'password and username match!',
token: 'encrypted token goes here'
})
} else {
res.json({
success: false,
message: 'password and username do not match'
})
}
})
app.listen(8000, function () { console.log('server is listening') })