question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Request closed and no one error event is emmited

See original GitHub issue

I have a this sample

'use strict';

const express = require('express'),
  Busboy = require('busboy'),
  bodyParser = require('body-parser'),
  app = new express()
  ;

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.post('/upload', function (req, res, next) {

  const busboy = new Busboy({ headers: req.headers });

  busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {

    file.on('data', function(data) {
      console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
    });

    file.on('close', function() {
      console.log('File [' + fieldname + '] Closed');
    });

    file.on('error', function() {
      console.log('File [' + fieldname + '] Errored');
    });

    file.on('end', function() {
      console.log('File [' + fieldname + '] Finished');
    });

    const fStream = require('fs').createWriteStream(require('path').join(__dirname, 'tmp'));

    fStream.on('finish', function () {
      console.log('File stream for [' + fieldname + '] Finished');
    });

    fStream.on('end', function () {
      console.log('File stream for [' + fieldname + '] End');
    });

    fStream.on('close', function () {
      console.log('File stream for [' + fieldname + '] Closed');
    });

    fStream.on('error', function () {
      console.log('File stream for [' + fieldname + '] Errored');
    });

    file.pipe(fStream);

  });

  req.on('close', function() {
    console.log('Request closed');
    res.send('Closed');
  });

  busboy.on('error', function() {
    console.log('Busboy error');
    res.send('Finished');
  });

  busboy.on('close', function() {
    console.log('Busboy close');
    res.send('Finished');
  });

  busboy.on('finish', function() {
    console.log('Request finished');
    res.send('Finished');
  });

  req.pipe(busboy);

});

app.listen(3000);

Whn I tried do this curl -F "image=@/tmp/test/big_file.jpg" --limit-rate 128k http://192.168.0.105:3000/upload and after a few seconds press Ctrl+C I had in console something like this

File [image] got 16384 bytes
File [image] got 16384 bytes
File [image] got 16384 bytes
File [image] got 16384 bytes
File [image] got 16384 bytes
File [image] got 16384 bytes
Request closed

And that’s all. No one error messages. How do I need to catch event where request is terminated from a client?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
mscdexcommented, Jan 11, 2017

@ValYouW I’m not sure it matters. The stream objects (both req and busboy) will just get GC’ed at some point anyway, so calling req.unpipe(busboy) on 'aborted' doesn’t really gain you much since nothing else will happen with req after it’s aborted.

1reaction
Muqitocommented, Jan 9, 2017

Correct me if I am wrong, but the file is just a ReadableStream; and a ReadableStream stops piping when it receives null.

If you received a file and the request is being closed you can just remove the output path of the WriteableStream. The readstream won’t emit an error since it’s just waiting for more data. There is no problem for a ReadableStream to wait 10 seconds or even longer before it receives data. Same with a WriteableStream.

If they can’t pipe to eachother they just buffer up in the memory until they can.

So an event on the request is the way to go. Don’t get me wrong; I completely understand why “this would be easier” for your codebase. But it has nothing to do with a ReadableStream error that the file actually is.

But if you want to track how much progress you have done and at what % you stopped the request; you can store expectedData from request headers like this:

let expectedData = req.headers['content-length'];

and in your file on data-event; you can track your fetched data by adding data.length to a fetchedData variable like this:

fetchedData += data.length;

If you want to know more about streams; I would suggest this handbook:

Substack’s stream handbook on github

Read more comments on GitHub >

github_iconTop Results From Across the Web

No end event is emitted when aborting http/https request #31630
The change is deliberate, because 'end' signals a successful completion of a stream, and an aborted stream is not successful. @masnagam from ...
Read more >
How can I test the 'error' and 'close' events of a node server ...
I found that I can simulate a prematurely closed request by exiting the process without calling res.end() , this triggers the close event...
Read more >
15 Common Error Codes in Node.js and How to Fix Them
The ETIMEDOUT error is thrown by the Node.js runtime when a connection or HTTP request is not closed properly after some time.
Read more >
Using Event Emitters in Node.js - DigitalOcean
Event emitters are objects in Node.js that trigger an event by sending a message to signal that an action was completed.
Read more >
Window: error event - Web APIs - MDN Web Docs - Mozilla
The error event is fired on a Window object when a resource failed to load or couldn't be used — for example if...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found