Node call request call inside get

I am trying to use nodejs as a layer between my public website and a server inside our network.

I use express.js to create a simple REST api. The API endpoint should initiate a request call to the webservice and return a result.

But calling the request inside my function .get()does nothing.

I want to return the result from an attached call that will be returned.

the code:

// Dependencies
var express = require('express');
var bodyParser = require('body-parser');
var request = require('request');
//Port
var port = process.env.PORT || 8080;  

// Express
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());


// Routes
app.get('/invoice', function(req, res){
   res.send('Express is workiung on IISNode')
});


app.get('/invoice/api/costumer=:customerId&invoice=:invoiceId',  function(req, res){
       res.send('Customer ID: ' + req.params.customerId + ' Invoice ID: '+ req.params.invoiceId)

      var url = 'http://xxx/invapp/getinvoice?company='+req.params.customerId+'S&customerno=13968&invoiceno='+req.params.invoiceId+'';
      request('http://www.google.com', function (error, response, body) {
      res.send(body);

      })

});

 // Start server
app.listen(port);
console.log("API is running on port " + port);

Any suggestions?

+6
source share
3 answers

You can write this way

// Dependencies
var express = require('express');
var bodyParser = require('body-parser');
var request = require('request');
//Port
var port = process.env.PORT || 8080;  

// Express
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());


// Routes
app.get('/invoice', function(req, res){
   res.send('Express is working')
});


app.get('/invoice/api/costumer=:customerId&invoice=:invoiceId',  function(req, res){

      var url = 'http://xxx/invapp/getinvoice?company='+req.params.customerId+'S&customerno=13968&invoiceno='+req.params.invoiceId+'';
      request(url, function (error, response, body) {
        var data={
          body:body,
          customerID:req.params.customerId,
          invoiceID:req.params.invoiceId
        };
      res.send(data);
      });    
});

 // Start server
app.listen(port);
console.log("API is running on port " + port);
+2
source

Please find the snippet I'm using. Hope this helps you too.

var body="";
function callyourservice(customerId,invoiceId,callback) {
        var options = {
            uri : url + 'costumer=:customerId&invoice=:invoiceId',
            method : 'GET'
        }
        request(options, function (error, response, body) {

            console.log(response);
            if (!error && response.statusCode == 200) {
                res = body;
            }
            else {
                res = 'Not Found';
            }
            callback(res);
        });
    }



callyourservice("customerId value","invoiceId value", function(resp){

    body=JSON.stringify(resp);;
});

callyourservice get , app.get('/'){ }

+1

node, pipe

 var http = require('http');

 http.createServer(function(request, response) {
   var proxy = http.createClient(9000, 'localhost')
   var proxyRequest = proxy.request(request.method, request.url, request.headers);
   proxyRequest.on('response', function (proxyResponse) {
     proxyResponse.pipe(response);
   });
   request.pipe(proxyRequest);
 }).listen(8080);

 http.createServer(function (req, res) {
   res.writeHead(200, { 'Content-Type': 'text/plain' });
   res.write('request successfully proxied to port 9000!' + '\n' + JSON.stringify(req.headers, true, 2));
   res.end();
 }).listen(9000);

0

Source: https://habr.com/ru/post/1653150/


All Articles