Middleware to set response headers in express app
This middleware will help you to set up response headers in 3 different way.
- Static Headers (Value is fixed while configuring this Middleware)
- Dynamically calculated headers (Value is calculated dynamically)
- Copying value from response object
Middleware can be configured using following method which accepts IConfig
parameter
This method will return configured middleware function that sets headers on the response based on configurations.
This is interface for configuration parameters
interface IConfig {
staticHeaders?: IStaticValues;
dynamicHeaders?: IDynamicValues;
copyFromRequestHeaders?: string[];
}
staticHeaders
: This param is of typeIStaticValues
and can be used to configure header with static values.dynamicHeaders
: This param is of typeIDynamicValues
and can be used to configure headers with dynamic values.copyFromRequestHeaders
: This parameter is array of string and values from request headers will be copied to response headers.
interface IStaticValues {
[key: string]: string;
}
key
is header key and value will be header value.. plain and simple.
interface IDynamicValues {
[key: string]: (arg0: Request) => string;
}
key
is header key, value
is of type function
, it will evaluated and request
object and return string value will be assigned to header key.
Happy codding