http
The http source receives inputs via an http server.
The http server can be configured to accept HTTP or HTTPS connections. When it receives a request, it converts the request into an object as describes in the Inputs section. It will convert outputs into HTTP responses.
fnrunner Key
fnrun.source/http
Configuration
The following table describes the keys on the configuration object for the http source.
Key | Type | Default | Description |
---|---|---|---|
address | String | :8080 | The network address for the http server to listen on |
base64EncodeBody | Boolean | false | Indicates whether incoming requests are base64-encoded |
certFile | String | "" | Path to certificate file for establishing HTTP connections. keyFile must also be set to use HTTPS. |
keyFile | String | "" | Path to key file for establishing HTTP connections. certFile must also be set to use HTTPS. |
treatOutputAsBody | Boolean | false | Indicates whether an output from an fn should be set as a response body instead of interpreting the output as a response. |
outputHeaders | Object | Empty object | Entries are header / value pairs. Applied when fn output does not provide headers. |
ignoreOutput | Boolean | false | Indicates the output from the fn should not be included in the http response. |
shutdownGracePeriod | Duration | 10s | The period to wait for the server to shutdown before terminating it forcefully |
Inputs
Inputs generated by the http source are objects with the following keys.
Key | Type | Description |
---|---|---|
host | String | The host on which the URL is sought |
remoteAddress | String | The network address that sent the request |
method | String | The HTTP method (GET, POST, etc.) |
protocol | String | The protocol used to make the request |
contentLength | Integer | The length of the associated content |
url | String | The URI being requested |
body | String | The body of the request |
cookies | Object | Cookie name/value pairs associated with the request |
headers | Object | Collections of string values associated with each header key |
query | Object | Collections of string values associated with each query string parameter |
Outputs
If the http source is not configured with treatOutputAsBody: true
, the source
expects the output be an object with the following keys. The source will convert
the output into an http response and return it.
Key | Type | Description |
---|---|---|
headers | Object | Header/Value pairs. If none are provided, defaults to configured outputHeaders . |
body | Object | An object to print to string and return as response body. Ignored if ignoreOutput is enabled. |
statusCode | Integer | The response status code. Defaults to 200 OK . |
Examples
The following example demonstrates setting up an HTTP server that accepts
requests at :8080
. It copies the output of the fn.js
into the body of the
response and sets its Content-Type
to have value application/json
.
source:
fnrun.source/http:
treatOutputAsBody: true
outputHeaders:
content-type: application/json
middleware:
- fnrun.middleware/key: body
fn:
fnrun.fn/cli: node ./fn.js