Compare commits
4 Commits
agent/issu
...
agent/issu
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cabd767abe | ||
|
|
97c03033d4 | ||
|
|
0629c6c300 | ||
|
|
d8f2645fe4 |
23
index.js
23
index.js
@@ -3,12 +3,16 @@ const express = require('express');
|
|||||||
const app = express();
|
const app = express();
|
||||||
const PORT = 3000;
|
const PORT = 3000;
|
||||||
|
|
||||||
const todos = [
|
app.use(express.json());
|
||||||
|
|
||||||
|
let todos = [
|
||||||
{ id: 1, title: 'Buy groceries', done: false },
|
{ id: 1, title: 'Buy groceries', done: false },
|
||||||
{ id: 2, title: 'Walk the dog', done: true },
|
{ id: 2, title: 'Walk the dog', done: true },
|
||||||
{ id: 3, title: 'Read a book', done: false },
|
{ id: 3, title: 'Read a book', done: false },
|
||||||
];
|
];
|
||||||
|
|
||||||
|
let nextId = 4;
|
||||||
|
|
||||||
app.get('/healthz', (req, res) => {
|
app.get('/healthz', (req, res) => {
|
||||||
res.json({ ok: true });
|
res.json({ ok: true });
|
||||||
});
|
});
|
||||||
@@ -17,6 +21,23 @@ app.get('/todos', (req, res) => {
|
|||||||
res.json(todos);
|
res.json(todos);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.post('/todos', (req, res) => {
|
||||||
|
const { title } = req.body;
|
||||||
|
const todo = { id: nextId++, title, done: false };
|
||||||
|
todos.push(todo);
|
||||||
|
res.status(201).json(todo);
|
||||||
|
});
|
||||||
|
|
||||||
|
app.delete('/todos/:id', (req, res) => {
|
||||||
|
const id = parseInt(req.params.id, 10);
|
||||||
|
const index = todos.findIndex(t => t.id === id);
|
||||||
|
if (index === -1) {
|
||||||
|
return res.status(404).end();
|
||||||
|
}
|
||||||
|
todos.splice(index, 1);
|
||||||
|
res.status(204).end();
|
||||||
|
});
|
||||||
|
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
console.log(`Server listening on http://localhost:${PORT}`);
|
console.log(`Server listening on http://localhost:${PORT}`);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user