Conn ✎
High level props and methods for the current connection, it helps you to reduce the usage of low-level req
and res
values.
// register extension
Grown.use(require('@grown/conn'));
// register middleware
server.plug(Grown.Conn);
// new props/methods are available at `ctx`
server.mount(ctx => {
if (ctx.is_json) {
return ctx.json({
status: 'ok',
result: ctx.req_headers,
});
}
ctx.resp_body = `<dl>
<dt>host</dt>
<dd>${ctx.host}</dd>
<dt>content_type</dt>
<dd>${ctx.content_type}</dd>
<dt>accept_languages</dt>
<dd>${ctx.accept_languages.join(', ')}</dd>
</dl>`;
});
Click ▷ RUN above and then request the endpoint as
application/json
— or usethis link
to perform a regular request below.
Request
Props mixin
req_headers
— Return all the request headers as an object.is_xhr
— Returnstrue
if the connection was made through XHR (x-requested-with
).is_json
— Returnstrue
if the connection requested asapplication/json
.has_type
— Returnstrue
if the connection requested a known type.host
— Current connection host.port
— Current connection port.remote_ip
— Remote client's IP.method
— Normalized request method as it can be sent as_method
.params
— Combination ofpath_params
,query_params
andbody_params
merged.path_info
— Returnrequest_path
segments as array.path_params
— Return the path params as an object, if any.body_params
— Returns the body payload as an object, if any.request_path
— Returns the fixed path of requested resources.query_string
— Returns the raw query-string from the connection.query_params
— Returns thequery_string
parsed as an object.accept_charsets
— Returns all accepted charsets by the current connection.accept_encodings
— Returns all accepted encodings by the current connection.accept_languages
— Returns all accepted languages by the current connection.accept_types
— Returns all accepted types by the current connection.
Methods mixin
accept_charset(value)
— Returnstrue
if the connection accepts this charset.accept_encoding(value)
— Returnstrue
if the connection accepts this encoding.accept_language(value)
— Returnstrue
if the connection accepts this language.accept_type(value)
— Returnstrue
if the connection accepts this type.get_req_header(name[, defvalue])
— Return a single request header.put_req_header(name, value)
— Set or update a request header.delete_req_header(name)
— Remove request headers byname
.
Public methods static
$mixins()
— Request mixins to be exported throughctx
object.
Response
Props mixin
has_body
— Returnstrue
if the connection has a body defined.has_status
— Returnstrue
if the connection has an status defined.content_type
— Currentcontent-type
, default totext/html
.status_code
— Currentctx.res.statusCode
, default to200
.resp_body
— Current response body,null
if none.resp_charset
— Charset from the current connection, default toutf8
.resp_headers
— Return all the response headers as an object.
Methods mixin
get_resp_header(name)
— Return a single response header.put_resp_header(name, value)
— Set or update a response header.merge_resp_headers(headers)
— Extend response headers.delete_resp_header(name)
— Remove a single response headers.redirect(location[, timeout[, body]])
— Redirect to another resource. Iftimeout
is given, a<meta http-equiv="refresh">
will be sent, usebody
to append anything else in this case.json(value)
— Send the given value asapplication/json
and ends the connection.get_buffer(url[, options])
— Callsget_file
with options, returns aBuffer
from the resulting stream.get_json(url[, options[, encoding]])
— Callsget_buffer
, thenJSON.parse
the resulting buffer.get_body(url[, options[, encoding]])
— Callsget_buffer
, returns the raw string from the buffer.get_file(url[, filepath])
— Returns a readable stream from the givenurl
. Iffilepath
is given, then the stream is written to it.send_file(entry[, mimeType])
— Send the given file asbinary/octet-stream
, usemimeType
to setup a different MIME.send([body])
— Finish the current connection. Thebody
value can be an object, buffer or string otherwise.end([code[, message]])
— End the current connection. Thecode
should be a number, otherwise it will be treated asmessage
value.
Public methods static
$mixins()
— Response mixins to be exported throughctx
object.$before_render(ctx, template)
— Extend locals withctx.state
hook.
Private* methods static
_finishRequest(ctx[, body])
— Normalize the input given byctx.send()
calls._endRequest(ctx, code[, message])
— Normalize the input given toctx.end()
calls._fetchBody(value)
— This method is called byget_file
._cutBody(value)
— Trim the value passed throughset_body
debug._fixURL(location)
— Normalize any given value into a valid URL string.