UNPKG

6.78 kBMarkdownView Raw
1---
2title: Kettle Servers
3layout: default
4category: Kettle
5---
6
7Kettle includes two builtin grades, [`kettle.server`](#kettle.server) for defining a plain HTTP server, and a derived grade [`kettle.server.ws`](#kettle.server.ws) for defining a server
8capable of handling WebSockets endpoints. The former wraps the standard facilities of node.js for constructing an [`http.Server`](https://nodejs.org/api/http.html#http_class_http_server), and
9the latter wraps the facilities of the [ws WebSockets library](https://github.com/websockets/ws) for constructing a [`ws.Server`](https://github.com/websockets/ws/blob/master/doc/ws.md#new-wsserveroptions-callback). In both
10cases, the Kettle grades can either accept servers previously constructed and injected in configuration, or can take responsibility for constructing the native servers themselves (the default).
11
12<a id="kettle.server"></a>
13
14## Configuration options for a `kettle.server`
15
16The `kettle.server` grade accepts the following options which can usefully be configured by the user. Naturally by virtue of being a `fluid.component` there
17are numerous other parts of its lifecycle which can be customised, but these are the options principally supported for user configuration:
18
19<table>
20 <thead>
21 <tr>
22 <th colspan="3">Supported configurable options for a </code>kettle.server</code></th>
23 </tr>
24 <tr>
25 <th>Option</th>
26 <th>Type</th>
27 <th>Description</th>
28 </tr>
29 </thead>
30 <tbody>
31 <tr>
32 <td><code>members.expressApp</code></td>
33 <td><a href="http://expressjs.com/4x/api.html#app"><code>express</code></a></td>
34 <td>The express <a href="http://expressjs.com/4x/api.html#app">application</a> which this server is to be bound to. If this option is not overriden, the server will automatically construct one using the <code>express()</code> constructor.</td>
35 </tr>
36 <tr>
37 <td><code>members.httpServer</code></td>
38 <td><a href="https://nodejs.org/api/http.html#http_class_http_server"><code>http.Server</code></a></td>
39 <td>The node.js <a href="https://nodejs.org/api/http.html#http_class_http_server"><code>HTTP server</code></a> which this server is to be bound to. If this option is not overriden, the server will use the one extracted from the <code>expressApp</code> member</td>
40 </tr>
41 <tr>
42 <td><code>port</code></td>
43 <td><code>Number</code></td>
44 <td>The port number which this server is to listen on. Defaults to 8081.</td>
45 </tr>
46 <tr>
47 <td><code>rootMiddleware</code></td>
48 <td><a href="#structure-of-entries-in-a-middlewaresequence"><code>middlewareSequence</code></a></td>
49 <td>The group of middleware which is to be executed for every request handled by this server</td>
50 </tr>
51 <tr>
52 <td><code>components.middlewareHolder</code></td>
53 <td><code>Component</code></a></td>
54 <td>A plain component container for middleware that is intended to be resolvable throughout the server's component tree</td>
55 </tr>
56 <tr>
57 <td><code>events.onListen</code></td>
58 <td><a href="http://docs.fluidproject.org/infusion/development/InfusionEventSystem.html"><code>Event</code></a></td>
59 <td>An event fired once this server has started listening on its port. Fired with one argument, the server component itself</code></td>
60 </tr>
61 <tr>
62 <td><code>events.beforeStop</code></td>
63 <td><a href="http://docs.fluidproject.org/infusion/development/InfusionEventSystem.html"><code>Event</code></a></td>
64 <td>An event fired just before this server is about to be stopped. This is an opportunity to clean up any resource (e.g. close any open sockets). Fired with one argument, the server component itself</code></td>
65 </tr>
66 <tr>
67 <td><code>events.onStopped</code></td>
68 <td><a href="http://docs.fluidproject.org/infusion/development/InfusionEventSystem.html"><code>Event</code></a></td>
69 <td>An event fired after the server is stopped and the HTTP server is no longer listening. Fired with one argument, the server component itself</code></td>
70 </tr>
71 <tr>
72 <td><code>events.onContributeMiddleware</code></td>
73 <td><a href="http://docs.fluidproject.org/infusion/development/InfusionEventSystem.html"><code>Event</code></a></td>
74 <td>This event is useful for authors trying to integrate with 3rd-party express applications. This is a useful lifecycle point, before Kettle registers its own middleware to the express application,
75 for an external integrator to register their own middleware first, e.g. using <code>app.use</code>. Fired with one argument, the server component itself – typically only <code>that.expressApp</code> will be of interest to the listener</code></td>
76 </tr>
77 <tr>
78 <td><code>events.onContributeRouteHandlers</code></td>
79 <td><a href="http://docs.fluidproject.org/infusion/development/InfusionEventSystem.html"><code>Event</code></a></td>
80 <td>This event is useful for authors trying to integrate with 3rd-party express applications. This is a useful lifecycle point, before Kettle registers its own route handlers to the express application,
81 for an external integrator to register their own route handlers first, e.g. using <code>app.get</code> etc.. Fired with one argument, the server component itself – typically only <code>that.expressApp</code> will be of interest to the listener</code></td>
82 </tr>
83 </tbody>
84</table>
85
86<a id="kettle.server.ws"></a>
87
88## Configuration options for a `kettle.server.ws`
89
90A WebSockets-capable server exposes all of the configurable options supported by a `kettle.server` in addition to the ones in the table below:
91
92<table>
93 <thead>
94 <tr>
95 <th colspan="3">Supported configurable options for a </code>kettle.server.ws</code></th>
96 </tr>
97 <tr>
98 <th>Option</th>
99 <th>Type</th>
100 <th>Description</th>
101 </tr>
102 </thead>
103 <tbody>
104 <tr>
105 <td><code>wsServerOptions</code></td>
106 <td><code>Object</code></td>
107 <td>Any options to be forwarded to the constructor of the <a href="https://github.com/websockets/ws/blob/master/doc/ws.md#new-wsserveroptions-callback"><code>ws.Server</code></a>. Note that after construction, this server will
108 be available as the top-level member named <code>wsServer</code> on the overall component.</td>
109 </tr>
110 </tbody>
111</table>