Skip to content

Commit f537d39

Browse files
committed
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.
1 parent 33c85c2 commit f537d39

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ npm install simplatic-http-server
6969
const staticServer = new StaticServer(portNumber /*, servePath = process.cwd() */)
7070
```
7171

72-
3. Listen to a port:
72+
3. Listen to `portNumber`:
7373

7474
```js
75-
await staticServer.listen()
75+
await staticServer.listen(/* onListenCallback, onErrorCallback */)
76+
7677
console.log(`The static server listening on ${portNumber} ...`)
7778
```
7879

@@ -85,10 +86,10 @@ npm install simplatic-http-server
8586
)
8687
```
8788

88-
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.*
89+
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).*
8990

9091
5. Turn it off when no more needed:
9192

9293
```js
93-
await staticServer.shutdown()
94+
await staticServer.shutdown(/* callback */)
9495
```

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "simplatic-http-server",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "A very light-weight and very simple static HTTP server based on node's built-in http module",
55
"main": "dist/main.umd.js",
66
"types": "dist/types/main.d.ts",

src/main.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import http from 'http'
55

66
import debug from 'debug'
77

8+
// noinspection JSUnusedGlobalSymbols
89
/**
910
* Created at 1398/4/2 (2019/6/23).
1011
* @author {@link https://mirismaili.github.io S. Mahdi Mir-Ismaili}
@@ -53,18 +54,32 @@ export default class StaticServer {
5354
})
5455
}
5556

56-
listen(): Promise<void> {
57+
// noinspection JSUnusedGlobalSymbols
58+
listen(onListen: () => void = () => {}, onError: (err: Error) => void = () => {}): Promise<void> {
5759
return new Promise((resolve, reject) => {
58-
this.staticServer.on('error', reject)
59-
60-
this.staticServer.listen(this.port, resolve)
61-
}
62-
)
60+
try {
61+
this.staticServer.on('error', err => {
62+
onError(err)
63+
reject(err)
64+
})
65+
66+
this.staticServer.listen(this.port, () => {
67+
onListen()
68+
resolve()
69+
})
70+
} catch (err) {
71+
onError(err)
72+
reject(err)
73+
}
74+
})
6375
}
6476

65-
shutdown(): Promise<void> {
77+
shutdown(callback: (err?: Error) => void = () => {}): Promise<void> {
6678
return new Promise((resolve, reject) =>
67-
this.staticServer.close(err => err === undefined ? resolve() : reject(err))
79+
this.staticServer.close(err => {
80+
callback(err)
81+
err === undefined ? resolve() : reject(err)
82+
})
6883
)
6984
}
7085
}

0 commit comments

Comments
 (0)