-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
38 lines (27 loc) · 798 Bytes
/
index.js
File metadata and controls
38 lines (27 loc) · 798 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
38
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use((req, res, next) => {
console.log(`${req.method} request for '${req.url}' - ${JSON.stringify(req.body)}`);
next();
});
app.use(express.static('./public'));
app.use(cors());
let items = [];
app.get('/api', (req, res) => {
res.json(items);
});
app.post('/api', (req, res) => {
items.push(req.body);
res.json(items);
});
app.delete('/api/:id', (req, res) => {
items = items.filter(item => item.id.toLowerCase() !== req.params.id.toLowerCase());
res.json(items);
});
app.listen(3000);
console.log('Express app running on port 8080');
module.exports = app;