-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
37 lines (30 loc) · 907 Bytes
/
server.js
File metadata and controls
37 lines (30 loc) · 907 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
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
let todos = [];
app.get('/api/todos', (req, res) => {
res.json(todos);
});
app.post('/api/todos', (req, res) => {
const todo = req.body;
todos.push(todo);
res.status(201).json(todo);
});
app.put('/api/todos/:id', (req, res) => {
const { id } = req.params;
const updatedTodo = req.body;
todos = todos.map(todo => (todo.id === id ? updatedTodo : todo));
res.json(updatedTodo);
});
app.delete('/api/todos/:id', (req, res) => {
const { id } = req.params;
todos = todos.filter(todo => todo.id !== id);
res.status(204).send();
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});