Socket
Socket
Sign inDemoInstall

on-finished

Package Overview
Dependencies
1
Maintainers
2
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    on-finished

Execute a callback when a request closes, finishes, or errors


Version published
Weekly downloads
37M
decreased by-8.97%
Maintainers
2
Install size
15.1 kB
Created
Weekly downloads
 

Package description

What is on-finished?

The on-finished npm package is a utility to execute a callback when an HTTP request/response cycle is completed or finished. It is useful for logging, cleaning up resources, or performing actions after the response has been sent to the client.

What are on-finished's main functionalities?

Execute callback when response finishes

This code sample creates an HTTP server that listens on port 3000. For each request, it uses on-finished to execute a callback when the response is finished. The callback logs 'Response finished' to the console.

const onFinished = require('on-finished');
const http = require('http');

http.createServer((req, res) => {
  onFinished(res, (err, res) => {
    console.log('Response finished');
  });

  res.end('Hello World');
}).listen(3000);

Detect when request is closed by the client

This code sample demonstrates how to use on-finished to detect when an HTTP request is closed prematurely by the client, such as when the client navigates away from the page or cancels the request.

const onFinished = require('on-finished');
const http = require('http');

http.createServer((req, res) => {
  onFinished(req, (err, req) => {
    if (err && err.code === 'ECONNRESET') {
      console.log('Request closed by the client');
    }
  });

  res.end('Hello World');
}).listen(3000);

Other packages similar to on-finished

Readme

Source

on-finished

NPM Version Node.js Version Build Status Coverage Status

Execute a callback when a request closes, finishes, or errors.

Install

$ npm install on-finished

API

var onFinished = require('on-finished')

onFinished(res, listener)

Attach a listener to listen for the response to finish. The listener will be invoked only once when the response finished. If the response finished to to an error, the first argument will contain the error.

Listening to the end of a response would be used to close things associated with the response, like open files.

onFinished(res, function (err) {
  // clean up open fds, etc.
})

onFinished(req, listener)

Attach a listener to listen for the request to finish. The listener will be invoked only once when the request finished. If the request finished to to an error, the first argument will contain the error.

Listening to the end of a request would be used to know when to continue after reading the data.

var data = ''

req.setEncoding('utf8')
res.on('data', function (str) {
  data += str
})

onFinished(req, function (err) {
  // data is read unless there is err
})

onFinished.isFinished(res)

Determine if res is already finished. This would be useful to check and not even start certain operations if the response has already finished.

onFinished.isFinished(req)

Determine if req is already finished. This would be useful to check and not even start certain operations if the request has already finished.

Example

The following code ensures that file descriptors are always closed once the response finishes.

var destroy = require('destroy')
var http = require('http')
var onFinished = require('finished')

http.createServer(function onRequest(req, res) {
  var stream = fs.createReadStream('package.json')
  stream.pipe(res)
  onFinished(res, function (err) {
    destroy(stream)
  })
})

License

MIT

FAQs

Last updated on 17 Aug 2014

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc