From f537d3907141e74b7ffe054b7e159b5a820e107f Mon Sep 17 00:00:00 2001 From: "S. M. Mir-Ismaili" Date: Sat, 29 Jun 2019 19:03:07 +0430 Subject: [PATCH] feat(`listen()` & `shutdown()` methods): Optional callbacks were added to these 2 methods Optional callbacks were added to `listen()` & `shutdown()` methods to consumer be able to handle events even without `then()` method. --- README.md | 9 +++++---- package-lock.json | 2 +- package.json | 2 +- src/main.ts | 31 +++++++++++++++++++++++-------- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index fc464c2..8a9622b 100644 --- a/README.md +++ b/README.md @@ -69,10 +69,11 @@ npm install simplatic-http-server const staticServer = new StaticServer(portNumber /*, servePath = process.cwd() */) ``` -3. Listen to a port: +3. Listen to `portNumber`: ```js - await staticServer.listen() + await staticServer.listen(/* onListenCallback, onErrorCallback */) + console.log(`The static server listening on ${portNumber} ...`) ``` @@ -85,10 +86,10 @@ npm install simplatic-http-server ) ``` -4. Get access to a file in `servePath`. E. g: type in your browser's address bar: `http://127.0.0.1/dir/index.html`. *Note: The path of `index.html` must be `` `${servePath}/dir/index.html` `` on your local machine.* +4. Get access to a file in `servePath`. E. g. type in your browser's address bar: `http://127.0.0.1/dir/index.html` *(Note: The path of `index.html` must be `` `${servePath}/dir/index.html` `` on your local machine).* 5. Turn it off when no more needed: ```js - await staticServer.shutdown() + await staticServer.shutdown(/* callback */) ``` diff --git a/package-lock.json b/package-lock.json index 55c0729..3d1f973 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "simplatic-http-server", - "version": "1.0.0", + "version": "1.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index a26730f..2c087e9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "simplatic-http-server", - "version": "1.0.0", + "version": "1.1.0", "description": "A very light-weight and very simple static HTTP server based on node's built-in http module", "main": "dist/main.umd.js", "types": "dist/types/main.d.ts", diff --git a/src/main.ts b/src/main.ts index b77d675..aaa85da 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,6 +5,7 @@ import http from 'http' import debug from 'debug' +// noinspection JSUnusedGlobalSymbols /** * Created at 1398/4/2 (2019/6/23). * @author {@link https://mirismaili.github.io S. Mahdi Mir-Ismaili} @@ -53,18 +54,32 @@ export default class StaticServer { }) } - listen(): Promise { + // noinspection JSUnusedGlobalSymbols + listen(onListen: () => void = () => {}, onError: (err: Error) => void = () => {}): Promise { return new Promise((resolve, reject) => { - this.staticServer.on('error', reject) - - this.staticServer.listen(this.port, resolve) - } - ) + try { + this.staticServer.on('error', err => { + onError(err) + reject(err) + }) + + this.staticServer.listen(this.port, () => { + onListen() + resolve() + }) + } catch (err) { + onError(err) + reject(err) + } + }) } - shutdown(): Promise { + shutdown(callback: (err?: Error) => void = () => {}): Promise { return new Promise((resolve, reject) => - this.staticServer.close(err => err === undefined ? resolve() : reject(err)) + this.staticServer.close(err => { + callback(err) + err === undefined ? resolve() : reject(err) + }) ) } }