How to stop server to accept new connections in Express

Last updated on Oct 16, 2022 by Suraj Sharma



In this tutorial, you will learn how you can stop your node server from accepting new connections in Node and Express


The server.close() method stops the server from accepting any new HTTP connections. However, all the existing connections are kept.


Example


  const express = require('express');
  const app = express();
  const port = 3000;

  const server = app.listen(port);

  server.close(()=>{
    console.log("Server closed for new connections")
  });


Related Solutions


Rate this post


Suraj Sharma is the founder of Future Gen AI Services. He holds a B.Tech degree in Computer Science & Engineering from NIT Rourkela.