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 linkto perform a regular request below.
Request
Props mixin
req_headers— Return all the request headers as an object.is_xhr— Returnstrueif the connection was made through XHR (x-requested-with).is_json— Returnstrueif the connection requested asapplication/json.has_type— Returnstrueif 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_paramsandbody_paramsmerged.path_info— Returnrequest_pathsegments 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_stringparsed 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)— Returnstrueif the connection accepts this charset.accept_encoding(value)— Returnstrueif the connection accepts this encoding.accept_language(value)— Returnstrueif the connection accepts this language.accept_type(value)— Returnstrueif 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 throughctxobject.
Response
Props mixin
has_body— Returnstrueif the connection has a body defined.has_status— Returnstrueif 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,nullif 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. Iftimeoutis given, a<meta http-equiv="refresh">will be sent, usebodyto append anything else in this case.json(value)— Send the given value asapplication/jsonand ends the connection.get_buffer(url[, options])— Callsget_filewith options, returns aBufferfrom the resulting stream.get_json(url[, options[, encoding]])— Callsget_buffer, thenJSON.parsethe 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. Iffilepathis given, then the stream is written to it.send_file(entry[, mimeType])— Send the given file asbinary/octet-stream, usemimeTypeto setup a different MIME.send([body])— Finish the current connection. Thebodyvalue can be an object, buffer or string otherwise.end([code[, message]])— End the current connection. Thecodeshould be a number, otherwise it will be treated asmessagevalue.
Public methods static
$mixins()— Response mixins to be exported throughctxobject.$before_render(ctx, template)— Extend locals withctx.statehook.
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_bodydebug._fixURL(location)— Normalize any given value into a valid URL string.