-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
80 lines (73 loc) · 2.31 KB
/
server.js
File metadata and controls
80 lines (73 loc) · 2.31 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
69
70
71
72
73
74
75
76
77
78
79
80
const express = require("express");
const request = require("request-promise");
const app = express();
const PORT = process.env.PORT || 5000;
// const apiKey = "b02a399e7d0cdafd8f6dfe43ba8d9e76"
// const baseUrl = `http://api.scraperapi.com?api_key=${apiKey}&autoparse=true`;
const generateScraperUrl = (api_key) =>
`http://api.scraperapi.com?api_key=${api_key}&autoparse=true`;
app.use(express.json());
app.get("/", (req, res) => {
res.send("WELCOME TO THE AMAZON SCRAPPER API");
});
// get product details
app.get("/product/:productid", async (req, res) => {
const { productid } = req.params;
const { api_key } = req.query;
try {
const response = await request(
`${generateScraperUrl(
api_key
)}&url=https://www.amazon.com/dp/${productid}`
);
res.json(JSON.parse(response));
// the json.parse will make the result to be in json format
} catch (err) {
console.log(err);
}
});
// get product reviews
app.get("/product/:productid/reviews", async (req, res) => {
const { productid } = req.params;
const { api_key } = req.query;
try {
const response = await request(
`${generateScraperUrl(api_key)}&url=https://www.amazon.com/product-reviews/${productid}`
);
res.json(JSON.parse(response));
// the json.parse will make the result to be in json format
} catch (err) {
console.log(err);
}
});
// get product offers
app.get("/product/:productid/offers", async (req, res) => {
const { productid } = req.params;
const{api_key} = req.query
try {
const response = await request(
`${generateScraperUrl(api_key)}&url=https://www.amazon.com/gp/offer-listing/${productid}`
);
res.json(JSON.parse(response));
// the json.parse will make the result to be in json format
} catch (err) {
console.log(err);
}
});
// implementing a search functionality
app.get("/search/:searchQuery", async (req, res) => {
const { searchQuery } = req.params;
const{api_key} = req.query
try {
const response = await request(
`${generateScraperUrl(api_key)}&url=https://www.amazon.com/s?k=${searchQuery}`
);
res.json(JSON.parse(response));
// the json.parse will make the result to be in json format
} catch (err) {
console.log(err);
}
});
app.listen(PORT, () => {
console.log(`server is running on localhost: ${PORT}`);
});