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.

KeyTypeDefaultDescription
addressString:8080The network address for the http server to listen on
base64EncodeBodyBooleanfalseIndicates whether incoming requests are base64-encoded
certFileString""Path to certificate file for establishing HTTP connections. keyFile must also be set to use HTTPS.
keyFileString""Path to key file for establishing HTTP connections. certFile must also be set to use HTTPS.
treatOutputAsBodyBooleanfalseIndicates whether an output from an fn should be set as a response body instead of interpreting the output as a response.
outputHeadersObjectEmpty objectEntries are header / value pairs. Applied when fn output does not provide headers.
ignoreOutputBooleanfalseIndicates the output from the fn should not be included in the http response.
shutdownGracePeriodDuration10sThe 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.

KeyTypeDescription
hostStringThe host on which the URL is sought
remoteAddressStringThe network address that sent the request
methodStringThe HTTP method (GET, POST, etc.)
protocolStringThe protocol used to make the request
contentLengthIntegerThe length of the associated content
urlStringThe URI being requested
bodyStringThe body of the request
cookiesObjectCookie name/value pairs associated with the request
headersObjectCollections of string values associated with each header key
queryObjectCollections 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.

KeyTypeDescription
headersObjectHeader/Value pairs. If none are provided, defaults to configured outputHeaders.
bodyObjectAn object to print to string and return as response body. Ignored if ignoreOutput is enabled.
statusCodeIntegerThe 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

Edit this page on GitHub