Skip to content

Commit

Permalink
step up the basic setup to an ExpressServer at least
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Verhoelen committed Apr 16, 2019
1 parent 55bbb58 commit bd7c15f
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
lib
2 changes: 0 additions & 2 deletions index.ts

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
"name": "node-express-typescript-recipes",
"version": "0.0.1",
"description": "A cookbook full of recipes for developing web apps with Node.js and Express.js in TypeScript",
"main": "index.ts",
"main": "service/index.ts",
"engines": {
"node": ">=8.15.0",
"npm": ">=6.4.1"
},
"scripts": {
"start": "ts-node index.ts"
"start": "ts-node service/index.ts"
},
"dependencies": {
"express": "^4.16.2"
Expand Down
46 changes: 46 additions & 0 deletions service/Application.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { ExpressServer } from './ExpressServer'

export class Application {
public static async createApplication() {
const expressServer = new ExpressServer()
await expressServer.setup(8000)
Application.handleExit(expressServer)

return expressServer
}

private static handleExit(express: ExpressServer) {
process.on('uncaughtException', (err: Error) => {
console.error('Uncaught exception', err)
Application.shutdownProperly(1, express)
})
process.on('unhandledRejection', (reason: {} | null | undefined) => {
console.error('Unhandled Rejection at promise', reason)
Application.shutdownProperly(2, express)
})
process.on('SIGINT', () => {
console.info('Caught SIGINT')
Application.shutdownProperly(128 + 2, express)
})
process.on('SIGTERM', () => {
console.info('Caught SIGTERM')
Application.shutdownProperly(128 + 2, express)
})
process.on('exit', () => {
console.info('Exiting')
})
}

private static shutdownProperly(exitCode: number, express: ExpressServer) {
Promise.resolve()
.then(() => express.kill())
.then(() => {
console.info('Shutdown complete')
process.exit(exitCode)
})
.catch(err => {
console.error('Error during shutdown', err)
process.exit(1)
})
}
}
23 changes: 23 additions & 0 deletions service/ExpressServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as express from 'express'
import { Express} from 'express'
import { Server } from 'http'

export class ExpressServer {
private server?: Express
private httpServer?: Server

public async setup(port: number) {
const server = express()
this.httpServer = this.listen(server, port)
this.server = server
return this.server
}

public listen(server: Express, port: number) {
return server.listen(port)
}

public kill() {
if (this.httpServer) this.httpServer.close()
}
}
5 changes: 5 additions & 0 deletions service/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Application } from './Application'

Application.createApplication().then(() => {
console.info('The application was started! Kill it using Ctrl + C')
})
18 changes: 18 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"outDir": "./lib",
"allowJs": false,
"target": "es2017",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"strict": true,
"experimentalDecorators": true,
"jsx": "react",
"importHelpers": true,
"lib": [ "es2017", "dom" ]
},
"include": [
"./service/**/*"
]
}

0 comments on commit bd7c15f

Please sign in to comment.