-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathnode-server.js
More file actions
41 lines (31 loc) · 838 Bytes
/
Copy pathnode-server.js
File metadata and controls
41 lines (31 loc) · 838 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
36
37
// server.js
const jsonServer = require('json-server')
const server = jsonServer.create()
const router = jsonServer.router('db.json')
const middlewares = jsonServer.defaults()
server.use(middlewares)
server.listen(3001, () => {
console.log('JSON Server is running')
})
server.put('/customers/30000001', (req, res) => {
let body = [];
req.on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
body = JSON.parse(Buffer.concat(body).toString());
console.log(JSON.stringify(body));
if (body.age && body.age > 18) {
console.log("error de validación");
return res.send({
error: true,
validation: {
age: 'Debe ser menor de edad',
name: 'El nombre es incorrecto'
}
});
} else {
res.send('ok');
}
});
})
server.use(router)