SpaceNode
SpaceNode automatically parses request bodies based on the Content-Type header. Supports JSON, URL-encoded forms, and multipart file uploads.
| Content-Type | Result |
|---|---|
application/json | body = parsed JSON object |
application/x-www-form-urlencoded | body = parsed key-value object |
multipart/form-data | body = form fields, files = uploaded files |
| No Content-Type | Attempts JSON parse |
| Other | body = raw string |
// 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)
}
// 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 })
}
// 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 })
}
| Property | Type | Description |
|---|---|---|
fieldname | string | Form field name |
filename | string | Original file name |
mimetype | string | MIME type (e.g. image/png) |
data | Buffer | File contents |
size | number | Size in bytes |
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 })
}
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.
bodyLimit to accommodate expected file sizes. Files are held in memory as Buffers.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.