lws-body-parser

raw JSON →
3.0.1 verified Sat Apr 25 auth: no javascript

Body parsing middleware for lws (local-web-server) that wraps koa-bodyparser. Version 3.0.1 is the latest stable release, requiring Node >=12.17. It provides body parsing for incoming HTTP requests, supporting JSON, URL-encoded, and multipart form data. Key differentiator: integration with lws middleware stack, providing a simple configuration-based approach compared to raw koa-bodyparser. Releases follow lws versioning cadence, with updates primarily driven by underlying koa-bodyparser changes.

error Error [ERR_REQUIRE_ESM]: require() of ES Module ... from ... not supported.
cause Using require() with an ESM-only package (lws-body-parser v3).
fix
Use import() or switch to ESM: 'import bodyParser from "lws-body-parser"' and set "type": "module" in package.json.
error TypeError: ctx.request.body is undefined
cause Request body not parsed because content-type is missing or not supported.
fix
Ensure request has proper Content-Type header (e.g., application/json). Verify body-parser middleware is added before route handlers.
breaking v3 dropped CommonJS support. Require() will throw a MODULE_NOT_FOUND error.
fix Migrate to ESM: use 'import' instead of 'require()'.
gotcha Body parsing only applies to requests with content-type application/json, application/x-www-form-urlencoded, or multipart/form-data.
fix Ensure incoming requests have the correct Content-Type header. For custom types, configure koa-bodyparser options via lws-body-parser.
deprecated Options passed to lws-body-parser may change in future versions to align with koa-bodyparser updates.
fix Check official lws-body-parser docs for options; avoid depending on undocumented koa-bodyparser options.
npm install lws-body-parser
yarn add lws-body-parser
pnpm add lws-body-parser

Creates an lws server with body-parser middleware. Parses request body and echoes it back. Shows ESM import and basic middleware setup.

import { createServer } from 'lws'
import bodyParser from 'lws-body-parser'

const server = createServer()
server.use(bodyParser)

server.use((ctx) => {
  ctx.body = `Received: ${JSON.stringify(ctx.request.body)}`
})

server.listen(8000, () => {
  console.log('Server running on http://localhost:8000')
})