Backend()
The Backend
constructor lets you dynamically create new Fastly Backends for your Fastly Compute service.
Note: Dynamic backends are by default disabled at the Fastly service level. Contact Fastly Support to request dynamic backends on Fastly Services.
To disable the usage of dynamic backends, see enforceExplicitBackends.
Syntax
new Backend(backendConfiguration)
Note:
Backend()
can only be constructed withnew
. Attempting to call it withoutnew
throws aTypeError
.
Parameters
backendConfiguration
: An Object which contains all the configuration options to apply to the newly created Backend.
name
: string- The name of the backend.
- The name has to be between 1 and 254 characters inclusive.
- The name can be whatever you would like, as long as it does not match the name of any of the static service backends nor match any other dynamic backends built during a single execution of the application.
- Throws a
TypeError
if the value is not valid. I.E. The value is null, undefined, an empty string or a string with more than 254 characters.
target
: string- A hostname, IPv4, or IPv6 address for the backend as well as an optional port.
- The target has to be at-least 1 character.
- Throws a
TypeError
if the value is not valid. I.E. Is null, undefined, an empty string, not a valid IP address or host, or is the string::
hostOverride
: string optional- If set, will force the HTTP Host header on connections to this backend to be the supplied value.
- Throws a
TypeError
if the value is an empty string.
connectTimeout
: number optional- Maximum duration in milliseconds to wait for a connection to this backend to be established.
- If exceeded, the connection is aborted and a 503 response will be presented instead.
- Throws a
RangeError
if the value is negative or greater than or equal to 2^32
firstByteTimeout
: number optional- Maximum duration in milliseconds to wait for the server response to begin after a TCP connection is established and the request has been sent.
- If exceeded, the connection is aborted and a 503 response will be presented instead.
- Throws a
RangeError
if the value is negative or greater than or equal to 2^32
betweenBytesTimeout
: number optional- Maximum duration in milliseconds that Fastly will wait while receiving no data on a download from a backend.
- If exceeded, the response received so far will be considered complete and the fetch will end.
- Throws a
RangeError
if the value is negative or greater than or equal to 2^32
useSSL
: boolean optional- Whether or not to require TLS for connections to this backend.
dontPool
: boolean optional- Determine whether or not connections to the same backend should be pooled across different sessions.
- Fastly considers two backends “the same” if they're registered with the same name and the exact same settings.
- In those cases, when pooling is enabled, if Session 1 opens a connection to this backend it will be left open, and can be re-used by Session 2.
- This can help improve backend latency, by removing the need for the initial network / TLS handshake(s).
- By default, pooling is enabled for dynamic backends.
tlsMinVersion
: 1 | 1.1 | 1.2 | 1.3 optional- Minimum allowed TLS version on SSL connections to this backend.
- If the backend server is not able to negotiate a connection meeting this constraint, a 503 response will be presented instead.
- Throws a
RangeError
if the value is not 1, 1.1, 1.2, or 1.3
tlsMaxVersion
: 1 | 1.1 | 1.2 | 1.3 optional- Maximum allowed TLS version on SSL connections to this backend.
- If the backend server is not able to negotiate a connection meeting this constraint, a 503 response will be presented instead.
- Throws a
RangeError
if the value is not 1, 1.1, 1.2, or 1.3
certificateHostname
: string optional- Define the hostname that the server certificate should declare.
- Throws a
TypeError
if the value is an empty string.
caCertificate
: string optional- The CA certificate to use when checking the validity of the backend.
- Throws a
TypeError
if the value is an empty string.
ciphers
: string optional- List of OpenSSL ciphers to support for connections to this origin.
- If the backend server is not able to negotiate a connection meeting this constraint, a 503 response will be presented instead.
- List of ciphers supported by Fastly.
- Throws a
TypeError
if the value is an empty string.
sniHostname
: string optional- The SNI hostname to use on connections to this backend.
- Throws a
TypeError
if the value is an empty string.
clientCertificate
: object optional- The client certificate to provide for the TLS handshake
certificate
: string- The PEM certificate string.
key
: SecretStoreEntry- The
SecretStoreEntry
to use for the key, created viaSecretStore.prototype.get
or alteratively viaSecretStore.fromBytes
.
- The
httpKeepalive
: number optional- Enable HTTP keepalive, setting the timout in milliseconds.
tcpKeepalive
: boolean | object optional- Enable TCP keepalive. When an object, optionally setting the keepalive configuration options.
timeSecs
: number optional- Configure how long to wait after the last sent data over the TCP connection before starting to send TCP keepalive probes.
intervalSecs
: number optional- Configure how long to wait between each TCP keepalive probe sent to the backend to determine if it is still active.
probes
: number optional- Number of probes to send to the backend before it is considered dead.
grpc
: boolean optional- Experimental feature
- When enabled, sets that this backend is to be used for gRPC traffic.
- Warning: When using this experimental feature, no guarantees are provided for behaviours for backends that do not provide gRPC traffic.
All optional generic options can have their defaults set via setDefaultDynamicBackendConfig()
.
This includes all configuration options above except for name
, target
, hostOverride
, sniHostname
and grpc
.
Return value
A new Backend
object.
Examples
In this example an explicit Dynamic Backend is created and supplied to the fetch request, the response is then returned to the client.
/// <reference types="@fastly/js-compute" />
import { Backend } from "fastly:backend";
async function app() {
// For any request, return the fastly homepage -- without defining a backend!
const backend = new Backend({
name: 'fastly',
target: 'fastly.com',
hostOverride: "www.fastly.com",
connectTimeout: 1000,
firstByteTimeout: 15000,
betweenBytesTimeout: 10000,
useSSL: true,
tlsMinVersion: 1.3,
tlsMaxVersion: 1.3,
});
return fetch('https://www.fastly.com/', {
backend // Here we are configuring this request to use the backend from above.
});
}
addEventListener("fetch", event => event.respondWith(app(event)));