user2828251
user2828251

Reputation: 235

Node js and eBay API implemenataion

Im a node JS beginner and i need help with exporting the console log data into HTML page.

I found this great example at the GitHub: https://github.com/benbuckman/nodejs-ebay-api

my problem is this: I managed to implement it with my needs - but I don't manage to extract the data out of the console log ! I simply want to display it in the browser and not in the console log.

Any suggestions ?

Currently im working on the "single" example - here's my code so far, which has many errors:

// example simple request to FindingService:findItemsByKeywords

var ebay = require('../index.js');
var http = require('http');

var express = require('express');
var app = express();
var io = require('socket.io');


app.set('port', process.env.PORT || 5000);

app.get('/get', function(req, res) {
    console.log('inside get');
//  for avoiding crossbrowser-error
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
    res.header('Content-Type','application/json');
var params = {};

params.keywords = [ "cat"];
params['paginationInput.entriesPerPage'] = 10;
ebay.ebayApiGetRequest({
    serviceName: 'FindingService',
    opType: 'findItemsByKeywords',
    appId: 'MYAPPID',      // FILL IN YOUR OWN APP KEY, GET ONE HERE: https://publisher.ebaypartnernetwork.com/PublisherToolsAPI
    params: params,
   // filters: filters,
    parser: ebay.parseItemsFromResponse    // (default)
  },
  // gets all the items together in a merged array
  function itemsCallback(error, items) {
    if (error) throw error;

    console.log('Found', items.length, 'items');

    for (var i = 0; i < items.length; i++) {
      console.log('- ' + items[i].title);
    }  
  }
);
});


http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

console.log('Listening on port 5000...');

Upvotes: 2

Views: 2260

Answers (1)

dylants
dylants

Reputation: 23340

It looks like you're using Express, and specify a single GET API endpoint on the URL /get. From what I understand, you want to see the output from the browser and not just the console (the console you're able to see with the console.log messages). Express will return to the user who makes the HTTP request what you put in res.send(). So for example, if you were to add:

res.send("hello");

to the end of the `app.get' function, you would see "hello" in the browser. Note that you can only do that once, so you'll need to bundle all the information you want to send to the browser in an object, and then send that.

It looks like what you want to see is the data collected from within the itemsCallback function. So to make things simple, you could just send the items back. For instance:

function itemsCallback(error, items) {
    if (error) throw error;

    console.log('Found', items.length, 'items');

    for (var i = 0; i < items.length; i++) {
        console.log('- ' + items[i].title);
    }

    res.send(items);  // sends the items in the HTTP response
}

As a side note, you don't need to include the http module, and instead can start the server like this:

app.listen(app.get('port'), function() {
    console.log('Express server listening on port ' + app.get('port'));
});

Upvotes: 2

Related Questions