-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
64 lines (55 loc) · 1.31 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import express from 'express';
import { getAllTodos, getTodo, createTodo, toggleTodoComplete } from './todoRepository.js';
const port = 4000;
// Create the server object
const app = express();
app.use(express.json());
// Handle the GET /api/todos route
/**
* GET /api/todos
* Send all todos
*/
app.get('/api/todos', (req, res) => {
const todos = getAllTodos();
res.send(todos)
});
/**
* GET /api/todos/:id
* Send one todo
*/
app.get('/api/todos/:id', (req, res) => {
const id = req.params.id;
const todo = getTodo(id);
if (!todo)
res.status(404).send();
else
res.send(todo);
})
app.patch('/api/todos/:id/toggleComplete', (req, res) => {
const id = req.params.id;
const todo = toggleTodoComplete(id);
if (!todo)
res.status(404).send();
else
res.send(todo);
})
/**
* POST /api/todos
* Create a new todo
*/
app.post('/api/todos', (req, res) => {
const todo = req.body;
const newTodo = createTodo(todo);
res.status(201).send(newTodo);
});
// Serve static web pages
app.use(express.static("web"))
// Start listening
app.listen(port, () => console.log(`listening on port ${port}`))
// app.listen(port, function () {
// console.log(`listening on port ${port}`);
// })
// app.listen(port, callbackFunction)
// function callbackFunction() {
// console.log(`listening on port ${port}`);
// }