-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfake-server-api.js
78 lines (70 loc) · 1.62 KB
/
fake-server-api.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
const express = require('express');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(cors());
app.post('/api/register', (req, res, next) => {
if (req.body.email === 'test@test.com') {
res.status(201).json({
status: 'success',
token: '1234567'
});
} else {
res.status(400).json({
status: 'error'
});
}
});
app.post('/api/login', (req, res, next) => {
if (req.body.email === 'test@test.com') {
res.status(200).json({
status: 'success',
token: '1234567'
});
} else {
res.status(400).json({
status: 'Incorrect email and/or password.'
});
}
});
app.get('/api/status', (req, res, next) => {
if (!(req.headers && req.headers.authorization)) {
return res.status(400).json({
error: 'Missing Token'
});
}
// simulate token decoding
const header = req.headers.authorization.split(' ');
const token = header[1];
if (token === '1234567') {
res.status(200).json({
user: {
email: 'test@test.com',
token: '1234567'
},
});
} else {
res.status(401).json({
error: 'Invalid Token'
});
}
});
app.use((req, res, next) => {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
app.use((err, req, res, next) => {
res.status(err.status || 500);
res.json({
status: 'error',
error: err
});
});
app.listen(8081, () => {
console.log('App listening on port 8081');
});