How can I exit from a Node.js program if it's running?

Dung Do Tien Jul 06 2021 198

I have created a server basic with NodeJs as below:

const express = require('express')
const app = express()

app.get('/', (req, res) => {
  res.send('welcome!')
})

app.listen(3000, () => console.log('Server ready'))

Now I want to stop and exit this server when I want to. 

How can I do it?

Have 2 answer(s) found.
  • Q

    Quang Minh Hà Jul 06 2021

    You can use to command below to stop your program:

    process.exit()

    Note: process does not require a "require", it's automatically available.

  • W

    Watcharapong Jakkawannorasing Jul 06 2021

    Call the global process object's exit method:

    process.exit()

    process.exit([exitcode])

    Ends the process with the specified code. If omitted, exit uses the 'success' code 0.

    To exit with a 'failure' code:

    process.exit(1);

    The shell that executed the node should see the exit code as 1.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close