-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver_json.js
More file actions
35 lines (28 loc) · 812 Bytes
/
server_json.js
File metadata and controls
35 lines (28 loc) · 812 Bytes
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
var jsonServer = require('json-server');
// Returns an Express server
var server = jsonServer.create();
// Set default middlewares (logger, static, cors and no-cache)
server.use(jsonServer.defaults())
// Add custom routes
server.post('/login', function (req, res) {
req.on('data', function(data) {
var data = JSON.parse(data.toString());
if (data.username == 'admin' && data.password == '123') {
res.json({
name: 'Mr. Furrball',
isAuthenticated: true,
avatar: 'http://placekitten.com/g/200/200'
});
} else {
res.status(401).send({
errors: [],
message: 'Invalid credentials',
status: 401
});
}
});
});
// Returns an Express router
var router = jsonServer.router('db.json');
server.use(router);
server.listen(3100);