These are the beta docs for the upcoming v0.10.0 release.

echo() - Response Output Helper

  • Type: Function(...args: any[]) => string
  • Description: Converts values to strings and optionally writes to response
  • Global: Yes (available as echo() in templates)
  • API Method: api.echo(...args)

Note: While echo() can be used for any response output, it's particularly handy for quick debugging, similar to PHP's echo. Objects are automatically stringified, so there's no need to call JSON.stringify() unless you need special formatting.

Basic Usage

<!-- Quick debugging -->
<%= echo(someVariable) %>

<!-- Objects are automatically stringified -->
<%= echo({ hello: 'world' }) %>

<!-- Custom response in middleware -->
<%
if (someCondition) {
  // Only use JSON.stringify if you need specific formatting
  echo(JSON.stringify({ status: 'ok' }, null, 2))
  return
}
%>

Common Use Cases

Debugging

<!-- Quick variable inspection -->
<pre>
Data: <%= echo(data) %>
Params: <%= echo(params) %>
Form: <%= echo(formData) %>
</pre>

<!-- Debug points -->
<%= echo('>>> Checkpoint 1') %>

Custom Responses

/** @type {import('pocketpages').MiddlewareLoaderFunc} */
module.exports = function (api) {
  const { echo, request } = api

  // Handle special cases
  if (request.header('Accept') === 'application/json') {
    echo(JSON.stringify({ ok: true }))
    return {}
  }

  // Continue to template rendering
  return {
    /* ... */
  }
}

Function Inspection

<!-- See what a function looks like -->
<pre><%= echo(mysteriousFunction) %></pre>

echo() vs Logging

  • echo(): Response output and debugging

    <%= echo(data) %>  <!-- Shows in browser -->
    
  • dbg(), info(): Server-side logging

    <% dbg(data) %>    <!-- Shows in server console -->
    

See Also