You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lib/node_modules/@stdlib/net/http-server/README.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,8 +40,8 @@ var httpServer = httpServerFactory();
40
40
41
41
The function supports the following parameters:
42
42
43
-
-**options**: options.
44
-
-**requestListener**: callback to invoke upon receiving an HTTP request.
43
+
-**options**: options (_optional_).
44
+
-**requestListener**: callback to invoke upon receiving an HTTP request (_optional_).
45
45
46
46
To bind a request callback to a server, provide a `requestListener`.
47
47
@@ -54,7 +54,7 @@ function requestListener( request, response ) {
54
54
var httpServer =httpServerFactory( requestListener );
55
55
```
56
56
57
-
The function accepts the following options:
57
+
In addition to the options supported by [`http.createServer`][nodejs-http-create-server], the function accepts the following options:
58
58
59
59
-**port**: server port. Default: `0` (i.e., randomly assigned).
60
60
-**maxport**: max server port when port hunting. Default: `maxport=port`.
@@ -84,7 +84,7 @@ var httpServer = httpServerFactory( opts );
84
84
85
85
When provided a `maxport` option, a created server will search for the first available `port` on which to listen, starting from `port`.
86
86
87
-
#### httpServer( \[options,]done )
87
+
#### httpServer( done )
88
88
89
89
Creates an [HTTP][nodejs-http] server.
90
90
@@ -104,7 +104,6 @@ httpServer( done );
104
104
105
105
The function supports the following parameters:
106
106
107
-
-**options**: server options which are passed directly to [`http.createServer`][nodejs-http-create-server]. Which options are supported depends on the Node.js version. Older Node.js versions (e.g., <= v8.12.0) do not support an options object, and, for those versions, a provided options object is ignored.
108
107
-**done**: callback to invoke once a server is listening and ready to handle requests.
109
108
110
109
</section>
@@ -115,6 +114,7 @@ The function supports the following parameters:
115
114
116
115
## Notes
117
116
117
+
- Which server options are supported depends on the Node.js version. Older Node.js versions (e.g., <= v8.12.0) do not support an options object when calling [`http.createServer`][nodejs-http-create-server], and, for those versions, any options specific to Node.js are ignored.
118
118
- Port hunting can be useful in a microservice deployment. When a `port` is randomly assigned (`options.port=0`), if a server fails and is restarted, the server is unlikely to bind to its previous `port`. By allowing a constrained search, assuming no lower `ports` within a specified range have freed up in the meantime, the likelihood of listening on the same `port` is increased. A server can typically restart and bind to the same `port` faster than binding to a new `port` and re-registering with a microservice registry, thus minimizing possible service interruption and downtime.
* Returns a function which creates an HTTP server.
129
129
*
130
+
* ## Notes
131
+
*
132
+
* - In addition to options documented below, the function supports any options supported by `http.createServer`. Which server options are supported depends on the Node.js version. Older Node.js versions (e.g., <= v8.12.0) do not support an options object when calling `http.createServer`, and, for those versions, any options specific to Node.js are ignored.
@@ -35,12 +35,23 @@ var DEFAULTS = require( './defaults.json' );
35
35
vardebug=logger('@stdlib/net/http-server');
36
36
varSUPPORTS_OPTIONS=(parseInt(NODE_VERSION.split('.')[0],10)>=8);// TODO: this is an imperfect test, as options only added in v8.12.0/v9.6.0
37
37
38
+
varEXCLUDE_OPTIONS=[
39
+
'port',
40
+
'maxport',
41
+
'hostname',
42
+
'address'
43
+
];
44
+
38
45
39
46
// MAIN //
40
47
41
48
/**
42
49
* Returns a function which creates an HTTP server.
43
50
*
51
+
* ## Notes
52
+
*
53
+
* - In addition to options documented below, the function supports any options supported by `http.createServer`. Which server options are supported depends on the Node.js version. Older Node.js versions (e.g., <= v8.12.0) do not support an options object when calling `http.createServer`, and, for those versions, any options specific to Node.js are ignored.
54
+
*
44
55
* @param {Options} [options] - server options
45
56
* @param {NonNegativeInteger} [options.port=0] - server port
46
57
* @param {NonNegativeInteger} [options.maxport] - max server port
@@ -77,19 +88,23 @@ function factory() {
77
88
varhostname;
78
89
varoptions;
79
90
varnargs;
91
+
varsopts;
80
92
varopts;
81
93
varport;
82
94
varmax;
83
95
varerr;
96
+
varflg;
84
97
85
98
nargs=arguments.length;
99
+
sopts={};
86
100
opts={};
87
101
if(nargs===1){
88
102
if(isFunction(arguments[0])){
89
103
requestListener=arguments[0];
90
104
}else{
91
105
options=arguments[0];
92
106
err=validate(opts,options);
107
+
flg=true;
93
108
}
94
109
}elseif(nargs>1){
95
110
options=arguments[0];
@@ -98,10 +113,15 @@ function factory() {
98
113
thrownewTypeError(format('invalid argument. Request listener must be a function. Value: `%s`.',requestListener));
99
114
}
100
115
err=validate(opts,options);
116
+
flg=true;
101
117
}
102
118
if(err){
103
119
throwerr;
104
120
}
121
+
if(flg){
122
+
// Resolve any server-specific options which should be passed to `http.createServer`:
123
+
sopts=omit(options,EXCLUDE_OPTIONS);
124
+
}
105
125
if(opts.port===void0){
106
126
port=DEFAULTS.port;
107
127
}else{
@@ -131,7 +151,6 @@ function factory() {
131
151
* Creates an HTTP server.
132
152
*
133
153
* @private
134
-
* @param {Options} [options] - server options
135
154
* @param {Callback} done - function to invoke after creating a server
136
155
* @throws {TypeError} must provide a function
137
156
*
@@ -145,34 +164,20 @@ function factory() {
145
164
* }
146
165
* httpServer( done );
147
166
*/
148
-
functionhttpServer(options,done){
167
+
functionhttpServer(done){
149
168
varserver;
150
-
varnargs;
151
-
varopts;
152
-
varcb;
153
-
154
-
nargs=arguments.length;
155
-
if(nargs<2){
156
-
opts={};
157
-
cb=options;
158
-
}else{
159
-
opts=options;
160
-
if(!isObject(opts)){
161
-
thrownewTypeError(format('invalid argument. Options argument must be an object. Value: `%s`.',opts));
162
-
}
163
-
cb=done;
164
-
}
165
-
if(!isFunction(cb)){
166
-
thrownewTypeError(format('invalid argument. Callback argument must be a function. Value: `%s`.',cb));
169
+
170
+
if(!isFunction(done)){
171
+
thrownewTypeError(format('invalid argument. Callback argument must be a function. Value: `%s`.',done));
167
172
}
168
173
if(requestListener){
169
174
if(SUPPORTS_OPTIONS){
170
-
server=http.createServer(opts,requestListener);
175
+
server=http.createServer(sopts,requestListener);
171
176
}else{
172
177
server=http.createServer(requestListener);
173
178
}
174
179
}elseif(SUPPORTS_OPTIONS){
175
-
server=http.createServer(opts);
180
+
server=http.createServer(sopts);
176
181
}else{
177
182
server=http.createServer();
178
183
}
@@ -210,7 +215,7 @@ function factory() {
210
215
functiononListen(){
211
216
varaddr=server.address();
212
217
debug('HTTP server initialized. Server is listening for requests on %s:%d.',addr.address,addr.port);
0 commit comments