Conn

High level details for the current connection. ⚓

/* @runkit */
// 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}</dd>
  </dl>`;
});

Click ► RUN and try requesting the endpoint as application/json — or use this link to perform a regular request.

Request

Props mixin

  • req_headers — Return all the request headers as an object.
  • is_xhr — Returns true if the connection was made through XHR (x-requested-with).
  • is_json — Returns true if the connection requested as application/json.
  • has_type — Returns true 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 of path_params, query_params and body_params merged.
  • path_info — Return request_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 the query_string parsed as an object.
  • accept_charset(s) — If singular, returns true if the connection accepts this charset. As plural, will return all accepted charsets by the current connection.
  • accept_encoding(s) — If singular, returns true if the connection accepts this encoding. As plural, will return all accepted encodings by the current connection.
  • accept_language(s) — If singular, returns true if the connection accepts this language. As plural, will return all accepted languages by the current connection.
  • accept_type(s) — If singular, returns true if the connection accepts this type. As plural, will return all accepted types by the current connection.

Methods mixin

  • 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 by name.

Public methods static

  • $mixins() — Request mixins to be exported through ctx object.

Response

Props mixin

  • has_body — Returns true if the connection has a body defined.
  • has_status — Returns true if the connection has an status defined.
  • content_type — Current content-type, default to text/html.
  • status_code — Current ctx.res.statusCode, default to 200.
  • resp_body — Current response body, null if none.
  • resp_charset — Charset from the current connection, default to utf8.

Methods mixin

  • resp_headers() — Return all the response headers as an object.
  • 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. If timeout is given, a <meta http-equiv="refresh"> will be sent, use body to append anything else in this case.
  • json(value) — Send the given value as application/json and ends the connection.
  • send_file(entry[, mimeType]) — Send the given file as binary/octet-stream, use mimeType to setup a different MIME.
  • send([body]) — Finish the current connection. The body value can be an object, buffer or string otherwise.
  • end([code[, message]]) — End the current connection. The code should be a number, otherwise it will be treated as message value.

Public methods static

  • $mixins() — Response mixins to be exported through ctx object.
  • $before_render(ctx, template) — Extend locals with ctx.state hook.

Private* methods static

  • _finishRequest(ctx[, body]) — Normalize the input given by ctx.send() calls.
  • _endRequest(ctx, code[, message]) — Normalize the input given to ctx.end() calls.
  • _cutBody(value) — Trim the value passed through set_body calls.
  • _fixURL(location) — Normalize any given value into a valid URL string.

➯ Next: Extensions ⟩ GraphQL