Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
node.js服务器句柄请求回调函数在写入响应之前结束_Node.js_Http_Callback - Fatal编程技术网

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

node.js服务器句柄请求回调函数在写入响应之前结束,node.js,http,callback,Node.js,Http,Callback,我有一个带有HandlerRequest回调的http服务器,它在vm.runInNewContext中为每个请求运行另一个脚本。在vm.runInNewContext内部运行的脚本会发出一些异步http post请求,并仅在从post获取响应后才写入服务器响应。 因此,HandlerRequest回调的代码在写入服务器响应之前结束。 安全吗?还是有办法避免这种情况 下面是一些代码: var server = http.createServer(handleRequest); server.li

我有一个带有HandlerRequest回调的http服务器,它在vm.runInNewContext中为每个请求运行另一个脚本。在vm.runInNewContext内部运行的脚本会发出一些异步http post请求,并仅在从post获取响应后才写入服务器响应。 因此,HandlerRequest回调的代码在写入服务器响应之前结束。 安全吗?还是有办法避免这种情况

下面是一些代码:

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

    });

简单的解决方案:从VM脚本返回承诺(例如,使用)
script.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);
  });
}    

这就是node的全部概念。这些方法应该返回,以便服务器可以继续处理事件循环中的其他事件。只有在调用回调之后,它才会返回处理这个特定的请求。问题是vm.runInNewContext没有回调,并且无法确保响应结束。在我的实际代码中,并非所有脚本都是这样的,它们可能不会执行response.end(),在这种情况下,我希望在HandlerRequest中执行,但此时它已经在该上下文中运行完毕。