The new middleware.
import assert from 'assert'
import createServer, { json, IHttpRequest, IHttpResponse } from '@egomobile/http-server'
const app = createServer()
// custom error handler
async function handleLimitReached(request: IHttpRequest, response: IHttpResponse) {
request.writeHead(400)
request.write('Input is too big')
}
// maximum input size: 128 MB
app.post('/', json(), async (request: IHttpRequest, response: IHttpResponse) => {
assert.strictEqual(typeof request.body, 'object')
})
// maximum input size: 256 MB
app.put('/', json(256), async (request: IHttpRequest, response: IHttpResponse) => {
assert.strictEqual(typeof request.body, 'object')
})
// maximum input size: 384 MB
app.patch('/', json({ limit: 402653184 }), async (request: IHttpRequest, response: IHttpResponse) => {
assert.strictEqual(typeof request.body, 'object')
})
app.delete('/', json({ limit: 1048576, onLimitReached: handleLimitReached }), async (request: IHttpRequest, response: IHttpResponse) => {
// alternative:
// app.delete('/', json(1, handleLimitReached), async (request: IHttpRequest, response: IHttpResponse) => {
assert.strictEqual(typeof request.body, 'object')
})
Optional
onLimitReached: Nilable<HttpRequestHandler>Optional
onParsingFailed: Nilable<ParseErrorHandler>Generated using TypeDoc
Creates a middleware, that reads the whole input of the request stream, parses it as JSON UTF-8 string and writes the object to 'body' property of the request context.