Socket
Socket
Sign inDemoInstall

on-finished

Package Overview
Dependencies
Maintainers
2
Versions
8
Alerts
File Explorer

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
Weekly downloads
43M
increased by3.45%
Maintainers
2
Weekly downloads
 
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 27 May 2015

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

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc