forked from webappio/livechat-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
37 lines (31 loc) · 843 Bytes
/
App.js
File metadata and controls
37 lines (31 loc) · 843 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 app = express();
const port = process.env.PORT || 3000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
let todos = [];
// Get all todos
app.get('/todos', (req, res) => {
res.json(todos);
});
// Add a new todo
app.post('/todos', (req, res) => {
const todo = req.body;
todos.push(todo);
res.status(201).json(todo);
});
// Delete a todo by index
app.delete('/todos/:index', (req, res) => {
const index = req.params.index;
if (index >= 0 && index < todos.length) {
todos.splice(index, 1);
res.status(204).send();
} else {
res.status(404).send();
}
});
// Start the server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});