-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
48 lines (36 loc) · 1.2 KB
/
app.js
File metadata and controls
48 lines (36 loc) · 1.2 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
const bodyParser = require('body-parser');
const express = require('express');
const Users = require('./src/usersService');
const users = new Users();
const app = express();
const port = 3000;
app.use(bodyParser.json());
app.get('/', (req, res) => res.send('Hello World!'));
// Basic fake authentication
app.use((req, res, next) => {
const authToken = req.get('authorization');
if (authToken !== 'super-secret-token') {
res.status(401).json({ message: 'Unauthorized' });
}
next();
})
// Users REST CRUD endpoints
app.get('/users', async (req, res) => {
res.json({ data: await users.list() });
});
app.post('/users', async (req, res) => {
const { name, email } = req.body;
res.json({ data: await users.create(name, email) });
});
app.get('/users/:id', async (req, res) => {
res.json({ data: await users.get(req.params.id) });
});
app.put('/users/:id', async (req, res) => {
const { name, email } = req.body;
res.json({ data: await users.update(req.params.id, name, email) });
});
app.delete('/users/:id', async (req, res) => {
await users.remove(req.params.id);
res.json({ data: null });
});
app.listen(port, () => console.log(`node-uptime app listening at http://localhost:${port}`));