Skip to content

Commit 011fb9c

Browse files
committed
[Examples] Add pagination JS CommonJS Node example
1 parent 112d932 commit 011fb9c

5 files changed

Lines changed: 234 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
API_KEY=YOUR_API_KEY
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Node.js pagination JavaScript (CommonJS) example
2+
3+
## Usage
4+
5+
1. Build module
6+
7+
```bash
8+
cd ../../../ # Navigate to the root folder
9+
deno task npm
10+
```
11+
12+
2. Setup environment variables
13+
14+
- Duplicate `.env.example` and name it `.env`.
15+
- Replace `YOUR_API_KEY` with your SerpApi API key.
16+
17+
3. Install dependencies and run example
18+
19+
```bash
20+
cd examples/node/pagination_js_commonjs
21+
npm i
22+
npm start
23+
```
24+
25+
## Notes
26+
27+
- If you want to run the example without building the module, you can update
28+
`package.json` to depend on the published `serpapi` npm module instead:
29+
```json
30+
{
31+
"dependencies": {
32+
"dotenv": "*",
33+
"serpapi": "*" // Relies on the npm module
34+
},
35+
"scripts": {
36+
"start": "node example.js"
37+
}
38+
}
39+
```
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const Dotenv = require("dotenv");
2+
const { config, getJson } = require("serpapi");
3+
4+
Dotenv.config();
5+
const apiKey = process.env.API_KEY;
6+
7+
const extractLinks = (results) => results.map((r) => r.link);
8+
9+
const run = async () => {
10+
const params = {
11+
q: "Coffee",
12+
api_key: apiKey,
13+
};
14+
15+
// Pagination (async/await)
16+
let page1 = await getJson("google", params);
17+
console.log(
18+
"First page links",
19+
extractLinks(page1.organic_results),
20+
);
21+
let page2 = await page1.next?.();
22+
console.log(
23+
"Second page links",
24+
extractLinks(page2?.organic_results),
25+
);
26+
27+
// Pagination (callback)
28+
getJson("google", params, (page1) => {
29+
console.log(
30+
"First page links",
31+
extractLinks(page1.organic_results),
32+
);
33+
page1.next?.((page2) => {
34+
console.log(
35+
"Second page links",
36+
extractLinks(page2.organic_results),
37+
);
38+
});
39+
});
40+
41+
// Use global config
42+
config.api_key = apiKey;
43+
page1 = await getJson("google", { q: "Coffee" });
44+
page2 = await page1.next?.();
45+
console.log(
46+
"Second page links",
47+
extractLinks(page2?.organic_results),
48+
);
49+
50+
// Pagination loop (async/await)
51+
let links = [];
52+
let page = await getJson("google", { q: "Coffee" });
53+
while (page) {
54+
links.push(...extractLinks(page.organic_results));
55+
if (links.length >= 30) break;
56+
page = await page.next?.();
57+
}
58+
console.log(links);
59+
60+
// Pagination loop (callback)
61+
links = [];
62+
getJson("google", { q: "Coffee" }, (page) => {
63+
links.push(...extractLinks(page.organic_results));
64+
if (links.length < 30 && page.next) {
65+
page.next();
66+
} else {
67+
console.log(links);
68+
}
69+
});
70+
};
71+
72+
run();

examples/node/pagination_js_commonjs/package-lock.json

Lines changed: 113 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"dependencies": {
3+
"dotenv": "*",
4+
"serpapi": "../../../npm"
5+
},
6+
"scripts": {
7+
"start": "node example.js"
8+
}
9+
}

0 commit comments

Comments
 (0)