9

I'm trying to use nodejs as a layer between my public website and a server on the inside of our network.

I'm using express.js to create a simple REST api. The API endpoint should trigger a request call to a webservice, and return the result.

But the request call inside my .get() function doesn't do anything.

I want to return the result from the nested call to be returned.

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?

3
  • Why are you sending response two times? Commented Aug 31, 2016 at 12:40
  • 4
    You are using 'res.send' in the first line of your 'get' endpoint. That will send the response to the client. Remove this line, and you should be OK. Commented Aug 31, 2016 at 12:40
  • That did it! Wow that was easy. Thanks alot. Didn't knwo you weren't able to send twice. Commented Aug 31, 2016 at 12:42

3 Answers 3

2

You can write in 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);
Sign up to request clarification or add additional context in comments.

Comments

1

Please find the snippet I am using. Hope this helps for you as well.

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);;
});

You can write callyourservice inside a get method from client like app.get('/'){ }

Comments

0

You can try doing it the node way using 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);

You can find the reference here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.