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.
Common errors
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.
Warnings
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.
Install
npm install lws-body-parser yarn add lws-body-parser pnpm add lws-body-parser Imports
- default export wrong
const { bodyParser } = require('lws-body-parser')correctimport bodyParser from 'lws-body-parser' - class usage with lws wrong
const lws = require('lws'); const bodyParser = require('lws-body-parser').default;correctimport { createServer } from 'lws' import bodyParser from 'lws-body-parser' const server = createServer() server.use(bodyParser) - type imports
import type { BodyParserOptions } from 'lws-body-parser'
Quickstart
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')
})