Exit Express gracefully

541 views
Skip to first unread message

Masiar

unread,
Mar 20, 2012, 8:37:06 AM3/20/12
to Express
Hello everybody. Is there a way to exit an express server gracefully?
I know that in node we have process.exit(1); to exit from within the
server.

Thanks!

Paul Vencill

unread,
Mar 20, 2012, 11:19:45 AM3/20/12
to expre...@googlegroups.com
Any reason you can't use the same?


--
You received this message because you are subscribed to the Google Groups "Express" group.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/express-js?hl=en.


knc

unread,
Mar 20, 2012, 12:13:21 PM3/20/12
to expre...@googlegroups.com
Note that process.exit(1) is not actually graceful enough. It will shut-down your server even if it's in the middle of a request. Depending on whether you are using DB transactions etc. it might leave your data in an unstable state.

If you really want to do it more gracefully, do app.close() - which will prevent your app from accepting new HTTP requests, while existing requests will continue to be processed. After that, have a reasonable timeout and then do process.exit(1) to make sure you kill your node process after all current requests have finished processing.

Kishore.

Masiar

unread,
Mar 20, 2012, 5:06:23 PM3/20/12
to Express
Thanks a lot. So app.close() alone won't terminate the server, but
gracefully bring it to a state in which process.exit(1) won't be too
raw. My question is: I don't have any "process" variable in my server,
just this "var express = require('express')" from which I create the
server itself. Should I call .exit(1) on that variable?

Masiar

knc

unread,
Mar 20, 2012, 11:40:12 PM3/20/12
to expre...@googlegroups.com
"process" is available to you globally. Say, you have "app.js" which you run from the command line this way: node app.js

Inside app.js you will have access to process. Just simply do: process.exit(1) and this will kill the node process. 

Masiar

unread,
Mar 21, 2012, 4:54:43 AM3/21/12
to Express
It seems like is not really closing the server... I would like
something that really terminates the process, like when your server is
running and you do ctrl+c. Is there something like this?
Thanks
Reply all
Reply to author
Forward
0 new messages