Body Parsing

SpaceNode automatically parses request bodies based on the Content-Type header. Supports JSON, URL-encoded forms, and multipart file uploads.

Supported Content Types

Content-TypeResult
application/jsonbody = parsed JSON object
application/x-www-form-urlencodedbody = parsed key-value object
multipart/form-databody = form fields, files = uploaded files
No Content-TypeAttempts JSON parse
Otherbody = raw string

JSON (Default)

// Client sends: POST /api/users
// Content-Type: application/json
// {"name":"John","email":"john@mail.com"}

export function create({ body, send }) {
  console.log(body.name)   // "John"
  console.log(body.email)  // "john@mail.com"
  send(201, body)
}

URL-Encoded Forms

// Client sends: POST /contact
// Content-Type: application/x-www-form-urlencoded
// name=John&message=Hello

export function contact({ body, send }) {
  console.log(body.name)     // "John"
  console.log(body.message)  // "Hello"
  send({ received: true })
}

Multipart / File Upload

// Client sends: POST /upload
// Content-Type: multipart/form-data

export function upload({ body, files, send }) {
  console.log(body)   // { description: "My photo" }  — form fields
  console.log(files)  // [{ fieldname, filename, mimetype, data, size }]

  if (files) {
    for (const file of files) {
      console.log(file.filename)  // "photo.jpg"
      console.log(file.mimetype)  // "image/jpeg"
      console.log(file.size)      // 123456 (bytes)
      console.log(file.data)      // Buffer
    }
  }

  send({ uploaded: files?.length || 0 })
}

File Object Shape

PropertyTypeDescription
fieldnamestringForm field name
filenamestringOriginal file name
mimetypestringMIME type (e.g. image/png)
dataBufferFile contents
sizenumberSize in bytes

Cookies

Incoming cookies are parsed automatically and available as request.cookies:

export function handler({ cookies, send }) {
  console.log(cookies.token)  // "abc123"
  console.log(cookies.lang)   // "en"
  send({ theme: cookies.theme || 'dark' })
}

To set a cookie in the response, use cookie():

({ cookie, send }) => {
  cookie('token', 'abc123', { httpOnly: true, maxAge: 86400 })
  send({ ok: true })
}

Configurable Body Limit

By default, request bodies are limited to 1 MB. Configure via bodyLimit:

const app = await createApp({
  bodyLimit: 10 * 1024 * 1024,  // 10 MB (for file uploads)
})

Bodies exceeding the limit return 413 Request Entity Too Large.

Tip — For file uploads, increase bodyLimit to accommodate expected file sizes. Files are held in memory as Buffers.

Custom Body Parser

Replace the built-in parser with a custom one using setBodyParser() or app.setBodyParser(). The custom parser must return { body, files }:

import { createApp, setBodyParser } from 'spacenode'

// Global custom parser (before createApp)
setBodyParser(async (req, options) => {
  // Use busboy, formidable, multer, etc.
  return { body: parsedFields, files: parsedFiles }
})

// Or per-app:
const app = await createApp()
app.setBodyParser(async (req, options) => {
  return { body: {}, files: [] }
})

When a custom parser is set, the built-in JSON/URL-encoded/multipart parser is bypassed entirely. Your parser receives the raw Node.js IncomingMessage.