-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibraryServer.js
55 lines (42 loc) · 1.42 KB
/
libraryServer.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
const express = require('express');
const findAll = require('./mongodb')
const findByAuthor = require('./mongodb')
const insert = require('./mongodb')
const bodyParser = require('body-parser');
const app = express();
const jsonParser = bodyParser.json()
// landning
app.get('/', async function (req, res) {
res.send('<h1> This is an awesome library </h1>')
res.send('<h4> Check out complete Book list </h4>')
//res.sendFile( __dirname + "/" + "index.html" );
res.end()
});
// get all the books
app.get('/books', async function (req, res) {
let books = await findAll.findAll()
// show book list
// crate the html table
let result = '<table><tr><th>Name</th><th>Address</th>';
for (let book in books) {
result += "</tr><tr><td>" + books[book].author + "</td><td>" + books[book].title + "</td></tr>";
}
result += '</table>';
res.send(result);
});
//search by author
app.get('/books/:author', jsonParser, async function(req, res) {
let books = await findByAuthor.findByAuthor()
res.send(books);
})
// insert a book
app.post('/books', jsonParser, async function(req, res){
const author = req.body.author
const title = req.body.title
console.log(author, title)
const book = insert.insert({title, author})
res.end(JSON.stringify(book));
})
app.listen(8080, function(req, res) {
console.log("Server is running at port 8000");
});