简体   繁体   中英

Node.js - callback outside a callback?

I'm quite new to Node.js, so please bear with me...

Bascially, I have a proxy server, and I want to intercept HTTP body packets. These packets are fired off through zeromq to another process outside of Node.js (a C program) and the response (the modified packet), must be written back out as the response from the proxy. I hope I'm making sense.

I've commented the code with the bits I'm stuck with

Here is my javascript code:

var http = require('http');
var util=require('util');
var net=require('net');
var context = require('zmq');

// socket to talk to server
util.puts("Connecting to server...");
var requester=context.createSocket('req');

requester.on("message",function(reply) {
        util.puts("Received reply"+reply.toString());

        //response.write(reply,'binary');   //how do I pick up the `response` variable inside the proxy callback and write with it?!
})

requester.connect("ipc://myfd.ipc")

process.on('SIGINT', function() {
        requester.close()
})




http.createServer(function(request, response) {
        var proxy=http.createClient(80,request.headers['host']);
        var proxy_request=proxy.request(request.method,request.url,request.headers);

        proxy_request.addListener('response',function (proxy_response){
                proxy_response.addListener('data',function(chunk){
                        //util.puts(chunk);

                        requester.send(chunk);   //ok, my C program can read chunks

                        response.write(chunk,'binary');   //I don't want to do this - I want to write the response of `requester.send(chunk)`
               });
                proxy_response.addListener('end',function(){
                        //util.puts("end");
                        response.end();
                });
                response.writeHead(proxy_response.statusCode,proxy_response.headers);
        });


        request.addListener('close',function(){
                //util.puts("close");
        });
        request.addListener('data',function(chunk){
                //util.puts("data");
                proxy_request.write(chunk,'binary');
        });
        request.addListener('end',function(){
                //util.puts("end");
                proxy_request.end();
        });
}).listen(8080);

I hope someone can help...

as Pointy alread has pointed out (i see what you did there ;) ), you need to make requester.send(chunk); asynchronous by adding a callback, which gets invoked as soon as your C Program is finished doing what it does. You also could do it non-async ( -> synchronous) by directly returning the value.

I'd dig in some tutorials about "how to make your own node.js module with bindings";

  1. Good Overview
  2. Simple Async Module by Isaac Schleuters himself

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM