-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
64 lines (54 loc) · 1.55 KB
/
server.js
File metadata and controls
64 lines (54 loc) · 1.55 KB
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
`use strict`;
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const todoFunctions = require('./server/todoFunctions');
const loginFunctions = require('./server/loginFunctions');
const portNumber = process.env.PORT || 3000;
app.use(bodyParser.json());
app.get('/api/todos', ({ query: { from, to } }, res) => {
return res.send(todoFunctions.getTodos());
});
app.post('/api/todos', ({ body }, res) => {
try {
const newTodo = todoFunctions.addTodo(body);
res.status(201).send(newTodo);
} catch (error) {
res.status(400).send(error.message);
}
});
app.get('/api/todos/:id', ({ params: { id } }, res) => {
try {
const todo = todoFunctions.getTodo(id);
res.send(todo);
} catch (error) {
res.status(404).send(error.message);
}
});
app.put('/api/todos/:id', ({ body, params: { id } }, res) => {
try {
const updatedTodo = todoFunctions.updateTodo(id, body);
res.send(updatedTodo);
} catch (error) {
res.status(404).send(error.message);
}
});
app.delete('/api/todos/:id', ({ params: { id } }, res) => {
todoFunctions.deleteTodo(id);
res.send();
});
app.post('/api/login', ({ body }, res) => {
try {
const hash = loginFunctions.login(body);
res.cookie('user', hash)
res.send();
} catch (error) {
res.status(401).send(error.message);
}
});
// Create link to Angular build directory
var distDir = __dirname + '/dist/angular-exercise';
app.use(express.static(distDir));
app.listen(portNumber, () =>
console.log(`Web Server Started on port ${portNumber}`)
);