Skip to content

Commit

Permalink
Consume node api using express js. (#111)
Browse files Browse the repository at this point in the history
* v2.7.7

* add example using express js

* remove yarn lock
  • Loading branch information
pajaydev authored Jul 19, 2020
1 parent 2d61b55 commit 3577818
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ The intent is to simplify the request process by handling the tedious logic. It'

* [Installation](#installation)
* [Usage](#usage)
* [Starter Guide](#starter-guide)
* [Using express js](#using-express-js)
* [API Details](#api-details)
* [Examples](#examples)
* [Starter Guide](#starter-guide)
* [Test](#test)
* [Issues](#issues)

Expand Down Expand Up @@ -45,6 +46,9 @@ let ebay = new eBay({

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

## Using Express js
You can consume these ebay node api's using [Express](https://expressjs.com/). You can checkout the sample app [here]().

## API details

### Without Auth flow
Expand Down
14 changes: 14 additions & 0 deletions demo/node-express/README.md
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
40 changes: 40 additions & 0 deletions demo/node-express/index.html
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>
13 changes: 13 additions & 0 deletions demo/node-express/package.json
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"
}
}
36 changes: 36 additions & 0 deletions demo/node-express/server.js
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}`));

0 comments on commit 3577818

Please sign in to comment.