11// Imports
22import ip from 'ip'
3+ import mongoose from 'mongoose'
4+ import format from 'date-fns/format'
35
46// App Imports
57import { PORT , NODE_ENV } from '../config/env'
@@ -8,19 +10,34 @@ import { PORT, NODE_ENV } from '../config/env'
810export default function ( server ) {
911 console . info ( 'SETUP - Starting server..' )
1012
13+ // Start Server
1114 server . listen ( PORT , ( error ) => {
1215 if ( error ) {
1316 console . error ( 'ERROR - Unable to start server.' )
1417 } else {
1518 console . info ( `INFO - Server started on` )
1619 console . info ( ` Local http://localhost:${ PORT } [${ NODE_ENV } ]` )
17- console . info ( ` Network http://${ ip . address ( ) } :${ PORT } [${ NODE_ENV } ] \n` )
20+ console . info ( ` Network http://${ ip . address ( ) } :${ PORT } [${ NODE_ENV } ]` )
21+ console . info ( ` Datetime ${ format ( new Date ( ) , 'YYYY-MM-DD hh:mm:ss a' ) } \n` )
1822 }
1923 } )
2024
21- process . on ( 'SIGTERM' , function ( ) {
22- server . close ( function ( ) {
23- console . log ( 'Finished all requests. Server stopped.' )
25+ // Stop Server
26+ for ( let signal of [ 'SIGINT' , 'SIGTERM' ] ) {
27+ console . log ( signal )
28+
29+ process . on ( signal , function ( ) {
30+ console . info ( 'INFO - Shutting down server..' )
31+
32+ serverProcess . close ( function ( ) {
33+ console . info ( 'INFO - Server has been shut down.' )
34+
35+ mongoose . connection . close ( false , ( ) => {
36+ console . info ( 'INFO - Database disconnected.' )
37+
38+ process . exit ( 0 )
39+ } )
40+ } )
2441 } )
25- } )
42+ }
2643}
0 commit comments