-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consume node api using express js. (#111)
* v2.7.7 * add example using express js * remove yarn lock
- Loading branch information
Showing
5 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Express js starter kit | ||
Consume `ebay-node-api` api's using node express js. | ||
|
||
|
||
## Run the example | ||
|
||
```shell | ||
npm install | ||
# start the server. | ||
npm start | ||
``` | ||
|
||
## Authentication. | ||
Replace your client id here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset='utf-8'> | ||
<meta http-equiv='X-UA-Compatible' content='IE=edge'> | ||
<title>Ebay node api demo</title> | ||
<meta name='viewport' content='width=device-width, initial-scale=1'> | ||
</head> | ||
<body> | ||
<h1>Ebay node api playground</h1> | ||
<input type="text" id="searchId" placeholder="Search for some items"/> | ||
<button type="submit" onclick="search()">search</button> | ||
<h4>Items: </h4> | ||
<div id="search-items"></div> | ||
</body> | ||
<script> | ||
const searchItems = document.getElementById('search-items'); | ||
function renderData(data){ | ||
const items = data[0].searchResult[0].item; | ||
items.forEach((searchItem) => { | ||
if(searchItem.title){ | ||
let element = document.createElement('div'); | ||
element.textContent = searchItem.title[0]; | ||
searchItems.appendChild(element); | ||
} | ||
}); | ||
} | ||
function search(){ | ||
const searchText = document.getElementById('searchId').value; | ||
// clear the existing items. | ||
searchItems.innerHTML = ''; | ||
fetch(`/search?keyword=${searchText}`) | ||
.then(response => response.json()) | ||
.then(data => { | ||
renderData(data); // prints actual data. | ||
}) | ||
.catch(error => console.log(error)); | ||
} | ||
</script> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "ebay-node-api-example", | ||
"version": "1.0.0", | ||
"main": "server.js", | ||
"license": "MIT", | ||
"dependencies": { | ||
"ebay-node-api": "^2.8.7", | ||
"express": "^4.17.1" | ||
}, | ||
"scripts": { | ||
"start" : "node server.js" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict'; | ||
const express = require('express'); | ||
const path = require('path'); | ||
const Ebay = require('ebay-node-api'); | ||
const app = express(); | ||
const port = 3000; | ||
|
||
const ebay = new Ebay({ | ||
clientID: "----Client id----" | ||
}); | ||
|
||
// load index.html | ||
app.get('/', (req, res) => res.sendFile(path.join(__dirname + '/index.html'))); | ||
|
||
// create a route to search items in eBay. | ||
app.use('/search', function(req, res){ | ||
const queryParam = req.query; | ||
// call the ebay api | ||
ebay.findItemsByKeywords({ | ||
keywords: queryParam.keyword, | ||
sortOrder: 'PricePlusShippingLowest', //https://developer.ebay.com/devzone/finding/callref/extra/fndcmpltditms.rqst.srtordr.html | ||
Condition: 3000, | ||
SoldItemsOnly: false, | ||
affiliate: { | ||
networkId: 9, | ||
trackingId: 1234567890 | ||
} | ||
}).then((data) => { | ||
return res.status(200).send(data); | ||
}, (error) => { | ||
return res.status(404).send(data); | ||
}); | ||
}); | ||
|
||
// listen to the port. | ||
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`)); |