-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
68 lines (52 loc) · 2.05 KB
/
server.js
File metadata and controls
68 lines (52 loc) · 2.05 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
65
66
67
68
const express = require('express');
const bodyParser = require('body-parser');
const swaggerUi = require('swagger-ui-express');
const YAML = require('yamljs');
const swaggerDocument = YAML.load('./swagger.yml');
require('dotenv').config()
const bookController = require('./controller/book.controller');
const userController = require('./controller/user.controller');
const app = express();
const port = process.env.PORT || 3000;
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.post('/api/login', (req, res) => {
userController.loginUser(req.body.user, req.body.pass, req.body.token).then(data => res.json(data));
})
app.get('/api/login', (req, res) => {
res.sendStatus(200);
});
app.get('/api/logout', (req, res) => {
userController.logoutUser(req.query.user, req.query.token).then(data => res.json(data));
});
app.get('/api/books/:author', (req, res) => {
bookController.getBooksByAuthor(req.params.author).then(data => res.json(data));
});
app.get('/api/books', (req, res) => {
bookController.getBooks().then(data => res.json(data));
});
app.get('/api/books/:author', (req, res) => {
bookController.getBooksByAuthor(req.params.author).then(data => res.json(data));
});
app.post('/api/book', (req, res) => {
bookController.createBook(req.body).then(data => res.json(data));
});
app.put('/api/book', (req, res) => {
bookController.updateBook(req.body.book).then(data => res.json(data));
});
app.get('/api/book/:id', (req, res) => {
bookController.getBook(parseInt(req.params.id)).then(data => res.json(data));
});
app.delete('/api/book/:id', (req, res) => {
bookController.deleteBook(parseInt(req.params.id)).then(data => res.json(data));
});
app.get('/api/page/:title', (req,res) => {
bookController.getPage(req.params.title).then(data => res.json(data));
});
app.get('/', (req, res) => {
res.send(`<h1>The noddy API. Of Books.</h1>`)
});
app.listen(port, () => {
console.log(`Server listening on the port ${port}`);
})