Log In

http

ModuleServer-Side

HTTP/1.1 server and utilities

This module includes a server that parses HTTP requests and delivers responses in text or binary formats. It includes support for streaming request/response bodies and access to the underlying TCP socket interface.

Documentation

httpserveoptionsListenOptionsonRequestRequestCallbackcallbackerrorstringnullsocketServerSocketfalsestatusMessagecodenumberstringmimeTypeextensionstringstringparseQueryquerystringobject

HTTP/1.1 server and utilities. ServerSocket and ClientSocket are defined in the socket module. parseQuery object values are strings or arrays of strings if more than one string value is specified for a key.

ListenOptionsaddress'0.0.0.0'stringportnumberbacklog50numbernameundefinedstringmaxHeaderSize8192number

If a request's header exceeds maxHeaderSize bytes, the server responds with a generic 431 Request Header Fields Too Large and disconnects the client socket.

RequestCallbackrequestRequestresponseResponsedataArrayBufferremainingnumbernull

If a function is returned, it is called with data received in the body of the request and the remaining number of bytes based on the request's Content-Length header. To buffer the request body, call request.slurp to generate this function.

RequestslurpcallbackbodyArrayBufferstringobjectjsonformatnullstringmaxSize8192numberdataArrayBufferremainingnumberprotocolstringmethodstringuristringpathstringquery`{}`objectheaders`{}`objectcookie`{}`object

query is the result of parseQuery run on uri's query string, if any. headers and cookie store only the last string value for a given key.

slurp returns a function for buffering the request body (up to maxSize bytes) that issues callback when complete. If format is 'utf8', 'url', or 'json', the body is converted to a string, parseQuery object, or json structure, respectively. Otherwise, body is an ArrayBuffer. If the max size is exceeded, callback is not issued and the server responds with a generic 413 Request Entity Too Large.

Responsesendbody''ArrayBufferstringheaders`{}`objectstringstatus200numbercallbackerrorstringundefinedendbody''ArrayBufferstringheaders`{}`objectstringstatus200numbercallbackerrorstringundefinedgenericstatus200numberheaders`{}`objectstringokfunctionerrorfunctionsocketClientSocket

The first call to send will send headers and initiate a chunked HTTP response (adding a Transfer-Encoding: chunked header unless overridden in the headers object, after which headers and status default to (and must be) null in calls to send and end. If headers is a string, it is interpreted as an extension and substituted with the corresponding Content-Type header. A response initiated with send must be completed with a call to end. generic is a convenience method that uses statusMessage(status) as the response body. ok and error are nullary convenience methods that call generic() and generic(400), respectively.