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
Start listening for requests on the provided port. If the server was started before, simpleS will get sessions from the `.sessions` file if they exist or they have a valid structure. If the server is already started, then simpleS will restart the server and will listen on the new provided port. Can have an optional callback. All connection in simpleS are kept alive and the restart can take few seconds, for closing alive http and ws connections. While restarting, no new connection will be accepted but existing connections will be still served. This method is called automatically when a new simpleS instance is created, it is not needed to call it explicitly on server creation. The purpose of this method is to provide a way to switch port or to start a stopped simpleS instance.
165
+
Start listening for requests on the provided port. If the server is already startedthen simpleS will restart the server and will listen on the new provided port. Can have an optional callback. All connection in simpleS are kept alive and the restart can take few seconds for closing alive http and ws connections. While restarting, no new connection will be accepted but existing connections will be still served. This method is called automatically when a new simpleS instance is created, it is not needed to call it explicitly on server creation. The purpose of this method is to provide a way to switch port.
166
166
167
167
```javascript
168
168
server.start(80, function () {
@@ -176,7 +176,7 @@ server.start(80, function () {
176
176
177
177
callback: function()
178
178
179
-
Stop the server. The existing sessions are saved to the `.sessions` file for further access. Can have an optional callback. All connection in simpleS are kept alive and the closing can take few seconds, for closing alive http and ws connections. While closing, no new connection will be accepted but existing connections will be still served. The purpose of this method is to provide a way for closing the server and save the existing session for future use.
179
+
Stop the server. Can have an optional callback. All connection in simpleS are kept alive and the closing can take few seconds for closing alive http and ws connections. While closing, no new connection will be accepted but existing connections will be still served.
180
180
181
181
```javascript
182
182
server.stop(function () {
@@ -186,54 +186,48 @@ server.stop(function () {
186
186
187
187
### <aname="server-host"/> Virtual Hosting
188
188
189
-
`.host(name)`
189
+
`.host(name[, config])`
190
190
191
191
name: string
192
192
193
-
simpleS can serve multiple domains on the same server and port, using `.host()` method it is simple to specify which host should use which routes. By default, simpleS has the main host which will route all existent routes of the simpleS instance, this is vital for one host on server or when it is needed a general behavior for incoming requests. Routing methods explained below are applicable on simpleS instance, for the main host, and on this method to define different hosts.
193
+
config: object
194
+
195
+
simpleS can serve multiple domains on the same server and port, using `.host()` method it is simple to specify which host should use which routes. By default, simpleS has the main host which will route all existent routes of the simpleS instance, this is vital for one host on server or when it is needed a general behavior for incoming requests. This method will create and configure a new host or will return an existing host with a changed configuration.
Make the host active, this method is called automatically when a new host is created, it is not needed to call it explicitly on host creation.
204
-
205
-
`.close()`
206
-
207
-
Closes all the child WebSocket hosts and make the host inactive.
208
-
209
-
`.destroy()`
210
-
211
-
Close the host and removes it from the server. Can not destroy the main host, for the main host all routes will be cleaned as it would be a new created host.
Change the configuration of the host. Possible attributes:
218
208
219
-
`compression: boolean // true` - switch the compression of the response content, default is true
209
+
`useCompression: boolean // true` - Switch the compression of the response content, default is true
220
210
221
-
`limit: number // 1048576` - set the limit of the request body in bytes, default is 1MB.
211
+
`requestLimit: number // 1048576` - Set the limit of the request body in bytes, default is 1MB.
222
212
223
-
`origins: array of strings // []` - set the origins accepted by the host. By default, the server will accept requests only from the current host. To accept requests from any origin use `'*'`, if this parameter is used as the first parameter then all next origins are rejected. `'null'` is used for local file system origin. These limitations will work for `HTTP``GET` and `POST` request and even for `WebSocket` requests. The current host should not be added in the list, it is accepted anyway.
213
+
`acceptedOrigins: array of strings // []` - Set the origins accepted by the host. By default, the server will accept requests only from the current host. To accept requests from any origin use `'*'`, if this parameter is used as the first parameter then all next origins are rejected. `'null'` is used for local file system origin. These limitations will work for `HTTP``GET` and `POST` request and even for `WebSocket` requests. The current host should not be added in the list, it is accepted anyway.
224
214
```javascript
225
215
['null', 'localhost', 'example.com'] // Will accept requests only from these 3 hosts
226
216
227
217
['*', 'example.com'] // Will accept requests from all hosts except 'example.com'
228
218
```
229
219
230
-
`referers: array of strings // []` - set the referers that can use the static resources of the host. By default, the server will response to all referers. To accept all referers except some specific the first parameter should be `*`. The current host should not be added in the list, it is served anyway. The server will respond with error 404 to unacceptable referers.
220
+
`acceptedReferers: array of strings // []` - Set the referers that can use the static resources of the host. By default, the server will response to all referers. To accept all referers except some specific the first parameter should be `*`. The current host should not be added in the list, it is served anyway. The server will respond with error 404 to unacceptable referers.
231
221
```javascript
232
222
['*', 'example.com'] // will respond to all referers except 'example.com'
233
223
234
224
['example.com', 'test.com'] // Will respond only to these 2 referers
235
225
```
236
226
227
+
`sessionTimeout: number // 3600` - Set the time to live of a session in seconds, default is 1 hour.
228
+
229
+
`sessionPassword: string // ""` - Set the password for encrypting the session data on the client. Should be set as a long random string to improve security. The password should be kept the same after server restart to be sure that all the client session data is available. Default is an empty string.
230
+
237
231
### <aname="server-templating"/> Templating
238
232
239
233
`.engine(engine)`
@@ -291,17 +285,6 @@ All the methods described below are applicable on each host independently (see [
291
285
*/
292
286
```
293
287
294
-
### <aname="host-route"/> General routing
295
-
`.route(type, route, result)`
296
-
297
-
type: 'all', 'del', 'get', 'put' or 'post'
298
-
299
-
route: array[strings] or string
300
-
301
-
result: function(connection) or string
302
-
303
-
Can add listeners for all types of routes. The methods described below are just shortcuts to this method. For better legibility use shortcuts.
304
-
305
288
### <aname="host-all"/> All Requests
306
289
307
290
`.all(route, result)`
@@ -352,6 +335,17 @@ result: function(connection) or string
352
335
353
336
Listen for `PUT` requests and uses a callback function with connection as parameter or a string for rendering (see `Connection.render()`).
354
337
338
+
### <aname="host-route"/> General routing
339
+
`.route(type, route, result)`
340
+
341
+
type: 'all', 'del', 'get', 'put' or 'post'
342
+
343
+
route: array[strings] or string
344
+
345
+
result: function(connection) or string
346
+
347
+
Can add listeners for all types of routes. The methods described below are just shortcuts to this method. For better legibility use shortcuts.
348
+
355
349
### <aname="host-error"/> Error Routes
356
350
357
351
`.error(code, result)`
@@ -425,11 +419,13 @@ serve.leave('all', [
425
419
426
420
### <aname="host-log"/> Logging
427
421
428
-
`.log(callback)`
422
+
`.log([stream, ]callback)`
423
+
424
+
stream: object(writable stream instance) or string
429
425
430
426
callback: function(connection)
431
427
432
-
Allows to log data about the established connections, will write data to the `process.stdout`using `console.log` interface. The callbackshould return data which will be shown in the console. The callback function is triggered on HTTP and WS requests.
428
+
Allows to log data about the established connections, will write data to the `process.stdout`stream or a defined writable stream, if the `stream` parameter is a string then the logger will write to file with the path described in the string. The `callback` parameter should return data which will be shown in the console. This function is triggered on new HTTP and WS requests.
433
429
434
430
```javascript
435
431
server.log(function (connection) {
@@ -443,7 +439,7 @@ The parameter provided in callbacks for routing requests is an object that conta
443
439
444
440
```javascript
445
441
{
446
-
body:'',
442
+
body:[],
447
443
cookies: {
448
444
user:'me',
449
445
pass:'password'
@@ -491,7 +487,7 @@ The parameter provided in callbacks for routing requests is an object that conta
491
487
492
488
#### <aname="http-connection-body"/> .body
493
489
494
-
The content of the body of the request, for `GET` requests it is empty, for `POST` request it will contain plain data, parsed data is contained in `connection.query` or `connection.files`.
490
+
The content of the body of the request, for `GET` requests it is empty, for `POST` request it will contain plain data, parsed data is contained in `connection.query` or `connection.files`, this data is represented by a buffer.
495
491
496
492
#### <aname="http-connection-cookies"/> .cookies
497
493
@@ -630,7 +626,7 @@ data: any value
630
626
631
627
replacer: array[numbers or strings] or function(key, value)
632
628
633
-
space: number of string
629
+
space: number or string
634
630
635
631
Writes preformatted data to the response stream and ends the response, implements the functionality of `JSON.stringify()` for arrays, booleans, numbers and objects, buffers and strings are sent as they are. Should not be used with `.write()` or `.end()` methods, but `.write()` method can be used before. Should be used only once.
636
632
@@ -672,15 +668,15 @@ The WebSocket host is linked to the current or the main HTTP host (see Virtual H
672
668
673
669
### <aname="ws-host"/> WebSocket Host
674
670
675
-
`.ws(location, [config, ]callback)`
671
+
`.ws(location[, config], listener)`
676
672
677
673
location: string
678
674
679
675
config: object
680
676
681
-
callback: function(connection)
677
+
listener: function(connection)
682
678
683
-
Create WebSocket host and listen for WebSocket connections. For security reasons only requests from the current host or local file system origins are accepted, to accept requests from another locations the `origins` parameter from the configuration object of the host must bedefined. Also, for additional security or logic separation, protocols should be provided in the `config` parameter, they should match on the server and on the client, the length of the message can be limited too by the value of `limit` parameter, default is 1MiB, the value is defined in bytes. The connection can be used in raw and advanced mode. The advanced mode allows an event based communication over the WebSocket connection, while the raw mode represents a low level communication, default is advanced mode. The callback function comes with the connection as parameter.
679
+
Create a WebSocket host and listen for WebSocket connections. For security reasons only requests from the current host or local file system origins are accepted, to accept requests from another locations the `origins` parameter from the configuration object of the host must be defined. Also, for additional security or logic separation, protocols should be provided in the `config` parameter, they should match on the server and on the client, the length of the message can be limited too by the value of `limit` parameter, default is 1MiB, the value is defined in bytes. The connection can be used in raw and advanced mode. The advanced mode allows an event based communication over the WebSocket connection, while the raw mode represents a low level communication, default is advanced mode. The callback function comes with the connection as parameter.
Restarts the WebSocket host with new configuration and callback. The missing configuration parameters will not be changed. This method is called automatically when a new WebSocket host is created, it is not needed to call it explicitly on WebSocket host creation.
697
+
Restarts the WebSocket host with new configuration and callback. The missing configuration parameters will not be changed.
702
698
703
699
```javascript
704
700
echo.open({
@@ -708,14 +704,9 @@ echo.open({
708
704
// Application logic
709
705
});
710
706
```
711
-
712
-
#### <aname="ws-host-close"/> .close()
713
-
714
-
Stops the WebSocket host. Will close all existing connections and will not receive new connections.
715
-
716
707
#### <aname="ws-host-destroy"/> .destroy()
717
708
718
-
Will stop the current WebSocket host and will remove it from the WebSocket hosts list.
709
+
Close all existing connections and remove the host from the WebSocket hosts list.
0 commit comments