RiserBob logoRiserBobv1.0.0
HomeDocsCLIRelease notes

Getting started

  • Introduction
  • Installation
  • Quick start
  • Project structure

Core concepts

  • Routing
  • Request
  • Responses
  • Error handling

Middleware

  • Middleware
  • Authentication
  • Logger and CORS

Data

  • Validation

Structure and tooling

  • Modules
  • CLI

Examples

  • Build a project
  • Mongoose
  • Drizzle ORM
  • Sequelize
  • JWT and cookies
  • File uploads

Packages

  • Cookies

Resources

  • Troubleshooting
  • Release notes
  • License
  • Security
  • Contributing
Core concepts

Responses

Build responses with the spongRes helpers, or return a standard Response.

A handler must return a Response. The spongRes helper builds one for you, and you can always return a standard Response instead.

TypeScript
bob.spongGet('/ok', async (spongReq, spongRes) => spongRes.json({ ok: true }))bob.spongGet('/raw', async () => new Response('plain text'))

The basics

HelperDescription
send(body)Sends a text body with the current status. The content type is text/plain; charset=utf-8 unless you set one.
json(data, status?)Sends JSON. The status defaults to the one set with status().
status(code)Sets the status and returns spongRes, so it can be chained.
header(name, value)Sets a header and returns spongRes, so it can be chained.
TypeScript
return spongRes  .status(201)  .header('X-Request-Source', 'docs')  .json({ created: true })

Status helpers

Every common status has a named helper that returns a ready Response. Success and error helpers take optional data and fall back to a sensible default body.

FamilyExamplesDefault body
2xx successSpongOK(data?), SpongCreated(data?), SpongAccepted(data?), SpongPartialContent(data?), SpongNoContent(){ message: 'OK' }, { message: 'Created' }, { message: 'Accepted' }, {} for the rest
3xx redirectsSpongMovedPermanently(url?), SpongFound(url?), SpongSeeOther(url?), SpongTemporaryRedirect(url?), SpongPermanentRedirect(url?)A Location header, / by default
4xx client errorsSpongBadRequest(data?), SpongUnauthorized(data?), SpongForbidden(data?), SpongNotFound(data?), SpongConflict(data?), SpongUnprocessableContent(data?), SpongTooManyRequests(data?){ error: 'Bad Request' }, { error: 'Unauthorized' }, { error: 'Forbidden' }, { error: 'Not Found' }, {} for the rest
5xx server errorsSpongInternalServerError(data?), SpongBadGateway(data?), SpongServiceUnavailable(data?){ error: 'Internal Server Error' }
TypeScript
bob.spongGet('/users/:id', async (spongReq, spongRes) => {  const user = await findUser(spongReq.params.id)   if (!user) return spongRes.SpongNotFound({ error: 'User not found' })   return spongRes.SpongOK(user)})

Use SpongUnprocessableContent (422) to answer a failed validation, and SpongCreated (201) after creating something. Every name follows the standard status text with a Spong prefix.

Pick one response shape

The helpers accept any data, so the shape of your JSON is yours to choose. Choose one and keep it everywhere. The example apps in these docs use this shape:

Output
// success{ "success": true, "data": { ... } } // failure{ "success": false, "message": "Task not found" } // validation failure{ "success": false, "message": "Validation failed", "errors": { "title": ["title is required"] } }
Statuses that cannot carry a body (204, 205, 304) make json() throw a TypeError, and a status outside 200 to 599 throws a RangeError. Use SpongNoContent() for an empty success.
If a handler finishes without returning a Response, RiserBob answers 500 and logs the mistake on the server. The usual cause is a missing return.
PreviousRequestNextError handling

On this page

  • The basics
  • Status helpers
  • Pick one response shape