node.jsapigeeusergridapigee127

Node.js:Unable to invoke pipe function


I am trying out a simple program to invoke a call to a backend & get the response back, here is my node.js code:

'use strict';

var util = require('util');
var http = require('http');
var request = require('request');
var stream = require('stream');


module.exports = {
 summary:summary
}

function summary(request, response) {
 var id = request.swagger.params.id.value;
 var url = "http://localhost:8080/test-org/myApp/summaries?q='id'";
 console.log('Executing request: '+url);
 request.get(url).pipe(response);
 };

However I get the following error:

curl http://localhost:10010/v1/customers/1123/summary
Executing request: http://localhost:8080/test-org/myApp/summaries?q='
id'
TypeError: Cannot call method 'pipe' of undefined
    at summary (C:\Apigee127\API-116\api\controllers\summary.js:17:19)
    at swaggerRouter (C:\Apigee127\API-116\node_modules\a127-magic\node_modules\
swagger-tools\middleware\2.0\swagger-router.js:114:18)
    at C:\Apigee127\API-116\node_modules\a127-magic\lib\middleware.js:82:9
    ....

Is there some npm package I am missing that I need to download?

-S


Solution

  • You are breaking scoping. You function takes request but you also have loaded var request = require('request').

    Change it such that you have:

       var request = require('request');
    
       function summary(req, res) {
          request.get(url).pipe(res);
       }