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 use this link to perform a regular request below.


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_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) — Returns true if the connection accepts this charset.
  • accept_encoding(value) — Returns true if the connection accepts this encoding.
  • accept_language(value) — Returns true if the connection accepts this language.
  • accept_type(value) — Returns true 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 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.
  • 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. 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.
  • get_buffer(url[, options]) — Calls get_file with options, returns a Buffer from the resulting stream.
  • get_json(url[, options[, encoding]]) — Calls get_buffer, then JSON.parse the resulting buffer.
  • get_body(url[, options[, encoding]]) — Calls get_buffer, returns the raw string from the buffer.
  • get_file(url[, filepath]) — Returns a readable stream from the given url. If filepath is given, then the stream is written to it.
  • 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.
  • _fetchBody(value) — This method is called by get_file.
  • _cutBody(value) — Trim the value passed through set_body debug.
  • _fixURL(location) — Normalize any given value into a valid URL string.