How to programmatically shut down an instance of Express.js?

Sometimes, we want to programmatically shut down an instance of Express.js.

In this article, we’ll look at how to programmatically shut down an instance of Express.js.

How to programmatically shut down an instance of Express.js?

To programmatically shut down an instance of Express.js, we can call the close method.

For instance, we write

const server = app.listen(3000);

const handler = () => {
  server.close();
};

to call the app.listen with the port number to return an Express server object.

Then we call server.close to shut down the Express instance in the handler function.

Conclusion

To programmatically shut down an instance of Express.js, we can call the close method.