File tree Expand file tree Collapse file tree 4 files changed +30
-14
lines changed Expand file tree Collapse file tree 4 files changed +30
-14
lines changed Original file line number Diff line number Diff line change @@ -69,10 +69,11 @@ npm install simplatic-http-server
69
69
const staticServer = new StaticServer(portNumber /*, servePath = process.cwd() */)
70
70
```
71
71
72
- 3 . Listen to a port :
72
+ 3 . Listen to ` portNumber ` :
73
73
74
74
```js
75
- await staticServer.listen()
75
+ await staticServer.listen(/* onListenCallback, onErrorCallback */)
76
+
76
77
console.log(`The static server listening on ${portNumber} ...`)
77
78
```
78
79
@@ -85,10 +86,10 @@ npm install simplatic-http-server
85
86
)
86
87
```
87
88
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) .*
89
90
90
91
5 . Turn it off when no more needed:
91
92
92
93
```js
93
- await staticServer.shutdown()
94
+ await staticServer.shutdown(/* callback */ )
94
95
```
Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " simplatic-http-server" ,
3
- "version" : " 1.0 .0" ,
3
+ "version" : " 1.1 .0" ,
4
4
"description" : " A very light-weight and very simple static HTTP server based on node's built-in http module" ,
5
5
"main" : " dist/main.umd.js" ,
6
6
"types" : " dist/types/main.d.ts" ,
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import http from 'http'
5
5
6
6
import debug from 'debug'
7
7
8
+ // noinspection JSUnusedGlobalSymbols
8
9
/**
9
10
* Created at 1398/4/2 (2019/6/23).
10
11
* @author {@link https://mirismaili.github.io S. Mahdi Mir-Ismaili }
@@ -53,18 +54,32 @@ export default class StaticServer {
53
54
} )
54
55
}
55
56
56
- listen ( ) : Promise < void > {
57
+ // noinspection JSUnusedGlobalSymbols
58
+ listen ( onListen : ( ) => void = ( ) => { } , onError : ( err : Error ) => void = ( ) => { } ) : Promise < void > {
57
59
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
+ } )
63
75
}
64
76
65
- shutdown ( ) : Promise < void > {
77
+ shutdown ( callback : ( err ?: Error ) => void = ( ) => { } ) : Promise < void > {
66
78
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
+ } )
68
83
)
69
84
}
70
85
}
You can’t perform that action at this time.
0 commit comments