node.js在回调问题中表达res.send

时间:2016-12-12 09:44:37

标签: javascript node.js sockets express

我有一个node.js脚本,它有两件事:

  1. 启动一个侦听来自浏览器的POST请求的http服务器。
  2. 启动套接字连接到Java服务器
  3. 当请求来自浏览器时,我将使用套接字将一些数据发送到java服务器,然后将数据从java服务器返回到http响应中的浏览器。

    它适用于第一个请求但在第二个请求中我将收到错误:

    Error: Can't set headers after they are sent.
    

    这是node.js脚本:

    var client = new net.Socket();
    var clientConnected = false;
    
    app.options('*', cors());
    
    app.use(function(req, res, next) {
        var data = new Buffer('');
        req.on('data', function(chunk) {
            // read buffer data from the browser in the POST request
            data = Buffer.concat([data, chunk]);
        });
        req.on('end', function() {
            req.rawBody = data;
            next();
        });
    });
    
    app.post('/xxxxxx', function(req, res) {
        res.header("Access-Control-Allow-Origin", "*");
    
        if (!clientConnected) {
            // if the socket is not connected, create it
            client.connect(G_PORT, G_ADDRESS, function() {
                clientConnected = true;
            });
    
            client.on('close', function() {
                clientConnected = false;
            });
    
            client.on('data',function(data) {
                // get result from the java server through socket
                res.send(data);
            });
    
            client.on('error', function(error){
                clientConnected = false;
            });
            res.send();
        }
        else {
            // the socket is created, just send the data to socket
            client.write(req.rawBody);
        }
    }); 
    

    错误指向行:

    res.send(data);
    

    in

    client.on('data',function(data)
    

    我很陌生,希望有人可以给我一些建议,谢谢:)

    更新:

    我每次完成一个请求时都试图关闭套接字,在这种情况下一切正常:

    if (true) {
        var client = new net.Socket();
        client.connect(G_PORT, G_ADDRESS, function() {
            client.write(data);
        });
        client.on('close', function(){
        });
    
        client.on('data',function(data){
            res.send(decodeData);
            client.destroy();
        });
    
        client.on('error', function(error){
        });
    }
    

    似乎问题与我的全局套接字实例???

    有关

2 个答案:

答案 0 :(得分:1)

错误"错误:发送后无法设置标头。"当你已经调用res.send时调用res.send来。

在您的帖子请求中,您有2个res.send

    client.on('data',function(data) {
        // get result from the java server through socket
        res.send(data);
    });

    client.on('error', function(error){
        clientConnected = false;
    });
    res.send();

当客户端处于“数据”状态时你也发送res.send,并且发生了错误。每个请求只需要一个res.send

答案 1 :(得分:0)

也许你应该尝试一下。

var client = new net.Socket();
var clientConnected = false;

app.options('*', cors());

app.use(function(req, res, next) {
    var data = new Buffer('');
    req.on('data', function(chunk) {
        // read buffer data from the browser in the POST request
        data = Buffer.concat([data, chunk]);
    });
    req.on('end', function() {
        req.rawBody = data;
        next();
    });
});

app.post('/xxxxxx', function(req, res) {
    res.header("Access-Control-Allow-Origin", "*");

    if (!clientConnected) {
        // if the socket is not connected, create it
        var buffer = '';
        client.connect(G_PORT, G_ADDRESS, function() {
            clientConnected = true;
        });

        client.on('close', function() {
            clientConnected = false;
        });

        client.on('data',function(data) {
            buffer += data;
        });

        client.on('error', function(error){
            clientConnected = false;
        });
        client.on('end', function() {
            res.send(buffer);
        });
    } else {
        // the socket is created, just send the data to socket
        client.write(req.rawBody);
        res.send(req.rawBody);
    }
});