title: How can I use callback function for returning api response in node js
body: <p>I am new to callback functions. Actually I am using request module in node js to fetch some info. I am confused how to return my response as Javascript is asynchronous in nature. I have the following codes.
  My <code>app.js</code> code is as follows:</p>
  
  <pre><code>var express = require('express');
  
  var app = express();
  
  const request = require('request');
  
  var call = require('./method');
  app.use(express.json());
  
  app.post('/call', function(request, response){
      var json = request.body;
      var p1 = json.p1;
      var p2 = json.p2;
      var p3 = json.p3;
      var p4 = json.p4;
      call.callApi(p1, p2, p3, p4);
  
      //response.send(request.body); 
  });
  app.listen(3000);
  </code></pre>
  
  <p>And my function file named as method.js is as follows:</p>
  
  <pre><code>const request = require('request');
  
  function callApi(p1, p2, p3, p4, callback) {
  
      var api = "http://test.roadcast.co.in:8088/api/route/" + p1 + "/" + p2 + "/" + p3 + "/" + p4;
      console.log(api);
      request(api, function(err, res, body) {
          var result = body;
          return callback(result, false);
          //console.log(body);
          //return result;
      });
  }
  
  module.exports = {
      callApi: callApi,
  };
  </code></pre>
  
  <p>And I am request a POST request by sending a json data in body as follows:</p>
  
  <pre><code>{ "p1":"28.7033129", "p2":"77.1542682", "p3":"28.709637", "p4":"77.291254" }
  </code></pre>
  
  <p>I am getting the results properly but just I want to return the reponse, please correct me where I am getting wrong.</p>