Skip to content

Instantly share code, notes, and snippets.

@pajaydev
Last active June 4, 2020 07:26
Show Gist options
  • Save pajaydev/b10b4d29bae85b441954d298a9e0d2c3 to your computer and use it in GitHub Desktop.
Save pajaydev/b10b4d29bae85b441954d298a9e0d2c3 to your computer and use it in GitHub Desktop.
Consume ebay node api using Express JS
<!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'>
<link rel='stylesheet' type='text/css' media='screen' href='main.css'>
<script src='main.js'></script>
</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>
</body>
<script>
function search(){
const searchText = document.getElementById('searchId').value;
fetch(`/search?keyword=${searchText}`)
.then(response => response.json())
.then(data => {
console.log(data); // prints actual data.
})
.catch(error => console.log(error));
}
</script>
</html>
const express = require('express');
const path = require('path');
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}`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment