Socket
Socket
Sign inDemoInstall

http-graceful-shutdown

Package Overview
Dependencies
Maintainers
1
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

http-graceful-shutdown

gracefully shuts downs http server


Version published
Weekly downloads
117K
decreased by-3.61%
Maintainers
1
Weekly downloads
 
Created

What is http-graceful-shutdown?

The http-graceful-shutdown npm package provides a way to gracefully shut down HTTP servers in Node.js applications. It ensures that the server stops accepting new connections and waits for existing connections to complete before shutting down, which helps in preventing data loss and ensuring a smooth shutdown process.

What are http-graceful-shutdown's main functionalities?

Basic Graceful Shutdown

This code demonstrates a basic setup for a graceful shutdown of an HTTP server. The server will stop accepting new connections and wait for existing connections to complete before shutting down.

const http = require('http');
const gracefulShutdown = require('http-graceful-shutdown');

const server = http.createServer((req, res) => {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
});

server.listen(3000, () => {
  console.log('Server running at http://127.0.0.1:3000/');
});

gracefulShutdown(server);

Custom Shutdown Handlers

This code demonstrates how to add custom shutdown handlers. The `onShutdown` option allows you to define tasks that should be completed before the server shuts down, such as cleaning up resources or logging.

const http = require('http');
const gracefulShutdown = require('http-graceful-shutdown');

const server = http.createServer((req, res) => {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
});

server.listen(3000, () => {
  console.log('Server running at http://127.0.0.1:3000/');
});

gracefulShutdown(server, {
  onShutdown: () => {
    return new Promise((resolve) => {
      console.log('Custom shutdown tasks');
      setTimeout(resolve, 1000);
    });
  }
});

Timeout for Shutdown

This code demonstrates how to set a timeout for the shutdown process. If the server does not shut down within the specified time, it will forcefully terminate.

const http = require('http');
const gracefulShutdown = require('http-graceful-shutdown');

const server = http.createServer((req, res) => {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
});

server.listen(3000, () => {
  console.log('Server running at http://127.0.0.1:3000/');
});

gracefulShutdown(server, {
  timeout: 30000 // 30 seconds
});

Other packages similar to http-graceful-shutdown

Keywords

FAQs

Package last updated on 11 Feb 2023

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