I am working in a project that uses Node.js for a Haraka (an smtp server) plugin.
This is Node.JS and I have a little problem whith callbacks. I haven't been able to convert this particular code to use a callback.
So, this is the code that I have:
exports.hook_data = function (next, connection) {
    connection.transaction.add_body_filter('', function (content_type, encoding, body_buffer) {
        var header = connection.transaction.header.get("header");
        if (header == null || header == undefined || header == '') return body_buffer;
        var url = 'https://server.com/api?header=' + header ;
        var request = require('request');
        request.get({ uri: url },
          function (err, resp, body) {
              var resultFromServer = JSON.parse(body);
              return ChangeBuffer(content_type, encoding, body_buffer, resultFromServer);
          }
        );
    });
    return next();
}
This code does not work because It doesn't wait the callback of the Request to continue. I need to finish the request before next();
And these are the requirements:
- At the end of exports.hook_datais mandatory to returnnext(). But it only have to return it after the request.
- I need to return a Bufferinadd_body_filterbut I need to create the buffer with information getted from a server.
- To make the Get Request I need to use a parameter (header) that I only have insideadd_body_filter.
So the issue is that I can not make the request before add_body_filter and put the result inside a callback because the parameter that I need to make the request are only inside add_body_filter.
Any advice please?
 
     
     
     
     
    