node upload file by stream incomplete

When I tried to write an method using http.request, and pipe a fs.createReadStream() into req, the file has been uploaded, but the content is incomplete

my code is like below:

```
    var req = http.request(options, function(res) {

    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        if(chunk) {
            resData += chunk;
        }  
    });
    res.on('end', function() {
        callback(null, {
            statusCode: res.statusCode,
            headers: res.headers,
            data: resData
        });
    });
});
req.on('errer', function() {
    console.log('error');
});

// judge if the data params is a path string or raw data
if(data) {
    if(fs.existsSync(data)) {
        fs.createReadStream(data).pipe(req);
    } else {
        req.write(data);
    }
};
req.end();
```

In my server which its put to, I found the file has been uploaded but incomplete.

Move the end part (after the comment starting with `// judge``) inside the 'end' listener.

As per the code posted over here you are creating a global variable resData which will not get reset when another request will be made.

You need to define the variable inside the callback function so your function will look like this:

    var req = http.request(options, function(res) {
    //Initialize resData here
    var resData = '';
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        if(chunk) {
            resData += chunk;
        }  
    });
    res.on('end', function() {
        callback(null, {
            statusCode: res.statusCode,
            headers: res.headers,
            data: resData
        });
    });
});
//I think you had a typo here
req.on('error', function() {
    console.log('error');
});