Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More

on-finished

Advanced tools

Socket logo

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
Maintainers
1
Created

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

FAQs

Package last updated on 22 Feb 2022

Did you know?

Socket

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