forked from a-atalla/Booktips-task-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (50 loc) · 1.38 KB
/
index.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
const express = require('express')
const logger = require('morgan')
const google = require('googleapis')
const books = google.books('v1')
// Initializing the App
const app = express()
app.use(logger('dev'))
// Enable CORS from client-side
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS')
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization, Access-Control-Allow-Credentials')
res.header('Access-Control-Allow-Credentials', 'true')
next()
})
// Routes
app.get('/', (req, res) => {
res.json({'state': 'Welcome to Google Search server'})
})
app.get('/search/:title', (req, res) => {
let bookTitle = req.params.title
let startIndex = req.query.start
let maxResults = req.query.max
console.log(startIndex)
books.volumes.list({
q: bookTitle,
startIndex,
maxResults
}, (err, result) => {
if (err) {
res.json(err)
} else {
res.json(result)
}
})
})
app.get('/books/:bookId', (req, res) => {
let bookId = req.params.bookId
books.volumes.get({volumeId: bookId}, function (err, result) {
if (err) {
res.json(err)
} else {
res.json(result)
}
})
})
// Starting the server
app.listen(3030, () => {
console.log('Server is running at http://127.0.0.1:3030')
})