FarmHand
FarmHand

Reputation: 771

Integrate JSON response into Node.js/Express.js response

When I type somedomain.com/some_api_url?_var1=1 into a browser, the response is {"1":"descriptive string"}, where 1 is a numerical index variable whose value could range from 1 to n. And where "descriptive string" is some text that summarizes what the index represents.

How can I integrate the JSON response from the somedomain.com/some_api_url?_var1=1 api url into the very simple Node.js and Express.js example below?

For testing purposes, the very simple app.js shown below returns "Hello World" when the user requests http : // localhost : 3000 from their web browser. What specific changes need to be made to the code below so that the web browser responds to the request with:

Index is: 1  
Description is:  descriptive string  

instead of responding with "Hello World"?

Here is the current code for app.js:

var express = require('express');
var http = require('http');
var app = express();

app.get('/', function (req, res) {
    res.send('Hello World!');
});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

Here is my current attempt, which results in the console printing Got a response: undefined, and with the browser remaining hung up because nothing is returned to the browser as a response:

var express = require('express');
var http = require('http');
var app = express();

app.get('/', function (req, res) {

    var url = 'somedomain.com/some_api_url?_var1=1';

    http.get(url, function(res){
        var body = '';

        res.on('data', function(chunk){
            body += chunk;
        });

        res.on('end', function(){
            var fbResponse = JSON.parse(body);
            console.log("Got a response: ", fbResponse.picture);
        });
    }).on('error', function(e){
          console.log("Got an error: ", e);
    });

});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

The get example code was adapted from the example at this link.

Upvotes: 0

Views: 1038

Answers (3)

Zohaib Ijaz
Zohaib Ijaz

Reputation: 22925

You actually forgot to return response res.send(data). Change you endpoint code like this. Also use different variable name for internal response object. I am using response here.

app.get('/', function (req, res) {

    var url = 'somedomain.com/some_api_url?_var1=1';

    http.get(url, function(resonse){
         var body = '';

         resonse.on('data', function(chunk){
             body += chunk;
         });

         resonse.on('end', function(){
             var body = JSON.parse(body);
             var text = '';
             for (var key in body){
                  text += 'Index is: ' + key + 
                          '\nDescription is:  ' + body[key] 
             }   

// The Description is:  "descriptive string"  
             console.log("Got a response: ", fbResponse);
             res.send(text);            
         });
    }).on('error', function(e){
        console.log("Got an error: ", e);
    });

});

Upvotes: 1

Hage Yaapa
Hage Yaapa

Reputation: 521

You seem to be mixing up Express, native HTTP module, and HTTP client.

Here is the serverside code for sending the response you are looking for.

var express = require('express');
var http = require('http');
var app = express();

// our "database"
var database = ['APPLE', 'BOOK', 'CAT', 'DOG', 'ELEPHANT'];

app.get('/', function (req, res) {

  // apply query parsing only of the query parameter is specified
  if ('itemId' in req.query) {
    var itemId = req.query.itemId;
    // index less than 0 will not yield anything from our "database"
    if (itemId < 0) {
      res.status(400).send('Invalid item id');
    // the item index corresponds to one of the items in the "database"
    } else if (itemId < database.length) {
      var result = 'Index: ' + itemId + '<br>Description: ' + database[itemId];
      res.send(result);
    // index exceeds the size of the array, so nothing will be found
    } else {
      res.status(404).send('Item not found');
    }
  // render the default homepage
  } else {
    res.send('Request format: http://localhost:3000/?itemId=n');
  }

});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

To see it in action, load http://localhost:3000/?itemId=0 in the browser. Valid itemId values are 0 to 4.

Upvotes: 0

user01
user01

Reputation: 901

Try this code with express 4.14.0

As @Zohaib-Ijaz pointed out, res is redefined and won't work for the res.send without a rename. This code also calls itself for demo purposes (so you can ignore app.get('/some_api_url', for the moment.

Then once the http.get is done, work with the object and print as you like. Keep in mind this code is not defensive against errors in the JSON.

var express = require('express');
var http = require('http');
var app = express();

const PORT = 3000;

app.get('/', function (req, res) {

  var url = `http://localhost:${PORT}/some_api_url?_var1=1`;

  http.get(url, function (resInner) {
    var body = '';

    resInner.on('data', function (chunk) {
      body += chunk;
    });

    resInner.on('end', function () {
      var fullResponse = JSON.parse(body); // {"343",:"I've heard 344 is more"}

      // code to pair the keys with their data in plain js
      var indexKeys = Object.keys(fullResponse);
      var replies = indexKeys.map((key) => {
        return `Index is ${key}\nDescription is ${fullResponse[key]}`;
      });

      //note this injection of a <pre> tag is just so modern browsers
      // will respect the newlines. it is not an HTML document or JSON
      res.send(
        `<pre>${replies.join("\n")}</pre>`
      );
    });
  }).on('error', function (e) {
    console.log("Got an error: ", e);
  });

});


app.get('/some_api_url', (req, res) => {
  var var1 = req.query.var1 || "343";
  var value = `I've heard ${parseInt(var1) + 1} is more`;
  var reply = {};
  reply[var1] = value;
  res.send(JSON.stringify(reply));
});

app.listen(PORT, function () {
  console.log(`Example app listening on port ${PORT}!`);
});

Upvotes: 0

Related Questions