Created
          October 4, 2017 07:19 
        
      - 
      
 - 
        
Save simenbrekken/f97e7a7be0b1dc06238c6d3ef0ff64ea to your computer and use it in GitHub Desktop.  
    Signal handling for node Express apps in Docker
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | const PORT = process.env.PORT || 8081 | |
| const HOST = process.env.HOST || 'localhost' | |
| const app = express() | |
| const server = app.listen(PORT, err => { | |
| if (err) { | |
| console.error(err) | |
| process.exit(1) | |
| } | |
| console.log(`Listening at http://${HOST}:${PORT}`) | |
| }) | |
| const handleSignal = (signal, message) => { | |
| process.on(signal, () => { | |
| console.info(message) | |
| server.close(err => { | |
| if (err) { | |
| console.error(err) | |
| process.exit(1) | |
| } | |
| process.exit() | |
| }) | |
| }) | |
| } | |
| handleSignal('SIGINT', 'Received Ctrl-C, shutting down gracefully...') | |
| handleSignal('SIGTERM', 'Container is stopping, shutting down gracefully...') | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment