Thursday, 11 May 2017

Error: write after end in NodeJs pipe

Now,I want to use NodeJS for proxying some cross-domain requests, the process is divided into two steps:

  • 1.the client request for Node server;

  • 2.the Node server transmit the request for target server.

I need the origin headers of the client's request because the cross-domain requests used CORS, so I use stream's pipe method, the code:

```

// the request is a module installed through npm
var request = require('request'); 

exports.setProxy = function proxy(app, localUrl, remoteUrl){
    app.use(localUrl, function(req, res){
        var data = req.body;

        req.pipe(request({         
            url: remoteUrl,
            form: data
        }, function(err, re, body) {
            console.log(err);
            res.send(body || {status: 'error'});
        }));
    });
};

``` But,there is a error:

error write after end

I search for some information, got the potential result that the req stream write to request() stream after the request() stream end.

Passing { end: false } as options to pipe was of no use,I dont't know what I can do, I need help...



via JiangangXiong

No comments:

Post a Comment