node.js服务器处理请求回调函数在写入响应之前结束

时间:2013-10-14 11:55:42

标签: node.js http callback

我有一个带有handleRequest回调的http服务器,它在vm.runInNewContext中为每个请求运行另一个脚本。在vm.runInNewContext中运行的脚本会生成一些异步的http post请求,并且只有在收到帖子的响应后才会写入服务器响应。 因此,handleRequest回调的代码在写入服务器响应之前结束。 安全吗?或者有办法避免这种情况吗?

以下是一些代码:

var server = http.createServer(handleRequest);
server.listen(8080);

var handleRequest = function (request, response) {
    // get request data...

    var context = {
        ServerRequest : request,
        ServerResponse : response
     };

     var stringScript = // a string with the script that posts data
     var script = vm.createScript(stringScript);
     script.runInNewContext({ context: context });
}

脚本字符串执行此操作:

var request = require('request');
var options = {....}
var req = request.get(options);

    req.on('response', function (res) {
            var chunks = [];
            res.on('data', function(chunk) {
                chunks.push(chunk);
            });

            res.on('end', function() {
                var buffer = Buffer.concat(chunks);
                var encoding = res.headers['content-encoding'];
                if (encoding == 'gzip') {
                    zlib.gunzip(buffer, function(err, decoded) {
                        // set response headers and write the response
                        context.ServerResponse.end(decoded.toString()); 
                    });
                } else if (encoding == 'deflate') {
                    zlib.inflate(buffer, function(err, decoded) {
                    // set response headers and write the response                            
                    context.ServerResponse.end(decoded.toString());
                    })
                } else {
                    // set response headers and write the response                                                    
                    context.ServerResponse.end(buffer.toString());
                }
            });

    });

1 个答案:

答案 0 :(得分:0)

简单解决方案:从VM脚本返回一个承诺(例如使用Q-libraryscript.runInNewContext将返回从VM脚本返回的任何内容。这样你就可以回复"当VM代码完成时。

// Script for VM
// I simplified it. Just resolve or reject the promise whenever you are done with your work
'use strict';

var defer = q.defer();

doABarrelRoll(function() {
  defer.resolve('RESULT');
});

defer.promise; // This line will return the promise.


从VM脚本返回值时,不需要任何返回构造。只要写下你想要的东西,让魔法发生。

// Script for current context
'use strict';

var server = http.createServer(handleRequest);
server.listen(8080);

var handleRequest = function (request, response) {
  // get request data...

  var context = {
    ServerRequest : request,
    ServerResponse : response
  };

  var stringScript = // a string with the script that posts data
  var script = vm.createScript(stringScript);
  var prom = script.runInNewContext({
    context: context,
    q: require('q'),
  });

  prom.done(function ($result) {
    console.log('VM finished with result: ' + $result);
  });
}