Skip to content

Commit 3577818

Browse files
authored
Consume node api using express js. (#111)
* v2.7.7 * add example using express js * remove yarn lock
1 parent 2d61b55 commit 3577818

File tree

5 files changed

+108
-1
lines changed

5 files changed

+108
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ The intent is to simplify the request process by handling the tedious logic. It'
1414

1515
* [Installation](#installation)
1616
* [Usage](#usage)
17+
* [Starter Guide](#starter-guide)
18+
* [Using express js](#using-express-js)
1719
* [API Details](#api-details)
1820
* [Examples](#examples)
19-
* [Starter Guide](#starter-guide)
2021
* [Test](#test)
2122
* [Issues](#issues)
2223

@@ -45,6 +46,9 @@ let ebay = new eBay({
4546

4647
Check out the [Starter Guide](https://pajaydev.github.io/ebay-node-api) documentation with examples to get started.
4748

49+
## Using Express js
50+
You can consume these ebay node api's using [Express](https://expressjs.com/). You can checkout the sample app [here]().
51+
4852
## API details
4953

5054
### Without Auth flow

demo/node-express/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Express js starter kit
2+
Consume `ebay-node-api` api's using node express js.
3+
4+
5+
## Run the example
6+
7+
```shell
8+
npm install
9+
# start the server.
10+
npm start
11+
```
12+
13+
## Authentication.
14+
Replace your client id here

demo/node-express/index.html

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset='utf-8'>
5+
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
6+
<title>Ebay node api demo</title>
7+
<meta name='viewport' content='width=device-width, initial-scale=1'>
8+
</head>
9+
<body>
10+
<h1>Ebay node api playground</h1>
11+
<input type="text" id="searchId" placeholder="Search for some items"/>
12+
<button type="submit" onclick="search()">search</button>
13+
<h4>Items: </h4>
14+
<div id="search-items"></div>
15+
</body>
16+
<script>
17+
const searchItems = document.getElementById('search-items');
18+
function renderData(data){
19+
const items = data[0].searchResult[0].item;
20+
items.forEach((searchItem) => {
21+
if(searchItem.title){
22+
let element = document.createElement('div');
23+
element.textContent = searchItem.title[0];
24+
searchItems.appendChild(element);
25+
}
26+
});
27+
}
28+
function search(){
29+
const searchText = document.getElementById('searchId').value;
30+
// clear the existing items.
31+
searchItems.innerHTML = '';
32+
fetch(`/search?keyword=${searchText}`)
33+
.then(response => response.json())
34+
.then(data => {
35+
renderData(data); // prints actual data.
36+
})
37+
.catch(error => console.log(error));
38+
}
39+
</script>
40+
</html>

demo/node-express/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "ebay-node-api-example",
3+
"version": "1.0.0",
4+
"main": "server.js",
5+
"license": "MIT",
6+
"dependencies": {
7+
"ebay-node-api": "^2.8.7",
8+
"express": "^4.17.1"
9+
},
10+
"scripts": {
11+
"start" : "node server.js"
12+
}
13+
}

demo/node-express/server.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
const express = require('express');
3+
const path = require('path');
4+
const Ebay = require('ebay-node-api');
5+
const app = express();
6+
const port = 3000;
7+
8+
const ebay = new Ebay({
9+
clientID: "----Client id----"
10+
});
11+
12+
// load index.html
13+
app.get('/', (req, res) => res.sendFile(path.join(__dirname + '/index.html')));
14+
15+
// create a route to search items in eBay.
16+
app.use('/search', function(req, res){
17+
const queryParam = req.query;
18+
// call the ebay api
19+
ebay.findItemsByKeywords({
20+
keywords: queryParam.keyword,
21+
sortOrder: 'PricePlusShippingLowest', //https://developer.ebay.com/devzone/finding/callref/extra/fndcmpltditms.rqst.srtordr.html
22+
Condition: 3000,
23+
SoldItemsOnly: false,
24+
affiliate: {
25+
networkId: 9,
26+
trackingId: 1234567890
27+
}
28+
}).then((data) => {
29+
return res.status(200).send(data);
30+
}, (error) => {
31+
return res.status(404).send(data);
32+
});
33+
});
34+
35+
// listen to the port.
36+
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`));

0 commit comments

Comments
 (0)