Funkster is a compositional server library. This package contains combinators to parse and set the HTTP Content-* headers.
Additional examples of how to build HTTP server apis with funkster can be found here.
Typscript is used to illustrate the examples.
$ npm install funkster-http-headers-accept
$ npm install && npm run build
$ npm run test
This module uses the content-type and content-disposition packages under the hood so essentially the same api applies for the parsing and setting the Content-Type
(using mediaType
instead of type
for readability) and Content-Disposition
headers.
The parsed headers object has the following type:
{
type?: {
mediaType: string,
parameters?: Object
},
length?: number,
language?: string,
encoding?: string,
location?: string,
disposition?: {
type?: string,
parameters?: Object
}
}
import * as http from 'http';
import { parseContentHeaders } from 'funkster-http-headers-content';
import { asRequestListener, Ok } from 'funkster-http';
const echoEncoding = parseContentHeaders(contentHeaders => Ok(contentHeaders.encoding));
const server = http.createServer(asRequestListener(echoEncoding));
// start the node HTTP server and send e.g. a GET with the Content-Encoding header set to 'gzip'.
The following combinators are available for setting the respective content headers.
setContentType(type: string | ContentType)
: See content-type for the parameters.setContentDisposition(filenameOrOptions?: string | Options, options?: Options)
: Uses the same api as content-disposition.setContentLength(length: number)
setContentLanguage(language: string)
setContentEncoding(encoding: string)
: The encoding
parameter should be one of ["gzip", "compress", "deflate", "identity", "br"]setContentLocation(location: string)
import * as http from 'http';
import { setContentType } from 'funkster-http-headers-content';
import { asRequestListener, Ok } from 'funkster-http';
const sendJsonContentType = setContentType({ mediaType: "application/json", parameters: { charset: "utf-8" } });
const server = http.createServer(asRequestListener(sendJsonContentType));
// start the node HTTP server and send e.g. a GET.
Icon funky by iconsmind.com from the Noun Project.