Skip to content

Commit 8dc8fb9

Browse files
committed
[Examples] Add pagination JS ESM Node example
1 parent 011fb9c commit 8dc8fb9

5 files changed

Lines changed: 232 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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Node.js pagination JavaScript (ESM) 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_esm
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+
"type": "module",
32+
"dependencies": {
33+
"dotenv": "*",
34+
"serpapi": "*" // Relies on the npm module
35+
},
36+
"scripts": {
37+
"start": "node example.js"
38+
}
39+
}
40+
```
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import * as Dotenv from "dotenv";
2+
import { config, getJson } from "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 params = {
10+
q: "Coffee",
11+
api_key: apiKey,
12+
};
13+
14+
// Pagination (async/await)
15+
let page1 = await getJson("google", params);
16+
console.log(
17+
"First page links",
18+
extractLinks(page1.organic_results),
19+
);
20+
let page2 = await page1.next?.();
21+
console.log(
22+
"Second page links",
23+
extractLinks(page2?.organic_results),
24+
);
25+
26+
// Pagination (callback)
27+
getJson("google", params, (page1) => {
28+
console.log(
29+
"First page links",
30+
extractLinks(page1.organic_results),
31+
);
32+
page1.next?.((page2) => {
33+
console.log(
34+
"Second page links",
35+
extractLinks(page2.organic_results),
36+
);
37+
});
38+
});
39+
40+
// Use global config
41+
config.api_key = apiKey;
42+
page1 = await getJson("google", { q: "Coffee" });
43+
page2 = await page1.next?.();
44+
console.log(
45+
"Second page links",
46+
extractLinks(page2?.organic_results),
47+
);
48+
49+
// Pagination loop (async/await)
50+
let links = [];
51+
let page = await getJson("google", { q: "Coffee" });
52+
while (page) {
53+
links.push(...extractLinks(page.organic_results));
54+
if (links.length >= 30) break;
55+
page = await page.next?.();
56+
}
57+
console.log(links);
58+
59+
// Pagination loop (callback)
60+
links = [];
61+
getJson("google", { q: "Coffee" }, (page) => {
62+
links.push(...extractLinks(page.organic_results));
63+
if (links.length < 30 && page.next) {
64+
page.next();
65+
} else {
66+
console.log(links);
67+
}
68+
});

examples/node/pagination_js_esm/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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"type": "module",
3+
"dependencies": {
4+
"dotenv": "*",
5+
"serpapi": "../../../npm"
6+
},
7+
"scripts": {
8+
"start": "node example.js"
9+
}
10+
}

0 commit comments

Comments
 (0)