-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
47 lines (38 loc) · 1.06 KB
/
server.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
const port = process.env.PORT || 8000;
const express = require('express');
const app = express();
const models = require('./models');
app.use(express.json());
app.get('/', (req, res) =>
res.json({
message: 'Welcome to Equess app',
}),
);
app.get('/bands', async (req, res) => {
const Band = models['Band'];
const bands = await Band.findAll();
res.send(bands);
});
app.get('/bands/:id', (req, res) => {
const id = parseInt(req.params.id);
const band = bands.find(band => band.id === id);
res.status(200).json(band);
});
app.post('/bands', (req, res) => {
bands.push(req.body);
res.status(200).json(bands);
});
app.put('/bands/:id', (req, res) => {
const id = parseInt(req.params.id);
let band = bands.find(band => band.id === id);
band.name = req.body.name;
band.genre = req.body.genre;
res.status(200).json(band);
});
app.delete('/bands/:id', (req, res) => {
const id = parseInt(req.params.id);
let band = bands.find(band => band.id === id);
band.splice(bands.indexOf(band), 1);
res.status(200).json(band);
});
module.exports = app;