Javascript node.js express res.send in回调问题

Javascript node.js express res.send in回调问题,javascript,node.js,sockets,express,Javascript,Node.js,Sockets,Express,我有一个node.js脚本,它包含两件事: 启动从浏览器侦听POST请求的http服务器 启动连接到java服务器的套接字 当浏览器发出请求时,我将使用套接字将一些数据发送到java服务器,然后以http响应的形式将数据从java服务器返回到浏览器 对于第一个请求,它可以正常工作,但在第二个请求中,我将得到错误: Error: Can't set headers after they are sent. 下面是node.js脚本: var client = new net.Socket();

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

  • 启动从浏览器侦听POST请求的http服务器
  • 启动连接到java服务器的套接字
  • 当浏览器发出请求时,我将使用套接字将一些数据发送到java服务器,然后以http响应的形式将数据从java服务器返回到浏览器

    对于第一个请求,它可以正常工作,但在第二个请求中,我将得到错误:

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

    我是个新手,希望有人能给我一些建议,谢谢:)

    更新:

    我尝试在每次完成一个请求时关闭插座,在这种情况下,一切正常:

    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){
        });
    }
    
    这个问题似乎与我的全局套接字实例有关?

    错误“错误:发送头后无法设置头”。在调用res时出现。在已调用某个时发送

    在您的post请求中,您有2个res发送

        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

    也许你应该试试

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

    谢谢你的快速回复。我试图注释掉第二个.send函数,但仍然有相同的错误。顺便说一句,第二个.send函数只有在第一次发出post请求时才会触发。在第一个请求中,我不会向服务器发送任何数据(仅创建套接字),因此回调中的.send函数不会被触发。这是正常的。当您的客户机处于“数据”状态时,您可以重新发送,数据的默认最大大小为8192。因此,如果您收到10000条消息,您将在client.on上收到两次('data',->您将调用两次res.send。您想通过post请求实现什么?我想使用套接字将数据从浏览器发送到服务器,然后等待服务器的结果,最后将结果发送回服务器。我尝试了此解决方案,这次错误消失了……但“end”回调永远不会触发d所以我的浏览器无法获取任何数据…尝试关闭侦听器并告诉我;)你的意思是尝试在关闭回调中添加res.send(缓冲区);我尝试过,但关闭回调从未触发,因为我的套接字在连接到服务器后将保持活动状态。。。。。。
    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);
        }
    });