-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy path01.get.js
51 lines (37 loc) · 990 Bytes
/
01.get.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
/*
Keyrt með:
node 01.get.js
Keyrir upp express vefjón sem leyfir að sækja lista af færslum eða staka færlslu
eftir id.
Með cURL:
> curl http://localhost:3000
[{"id":1,"title":"Foo"},{"id":2,"title":"Bar"}]
> curl http://localhost:3000/2
{"id":2,"title":"Bar"}
*/
const express = require('express');
const app = express();
const data = [
{ id: 1, title: 'Foo' },
{ id: 2, title: 'Bar' },
];
app.get('/', (req, res) => {
res.json(data);
});
app.get('/:id', (req, res) => {
const { id } = req.params;
const item = data.find(i => i.id === parseInt(id, 10));
if (item) {
return res.json(item);
}
return res.status(404).json({ error: 'Not found' });
});
function notFoundHandler(req, res, next) { // eslint-disable-line
console.warn('Not found', req.originalUrl);
res.status(404).json({ error: 'Not found' });
}
app.use(notFoundHandler);
const port = 3000;
app.listen(port, () => {
console.info(`Server running at http://localhost:${port}/`);
});