The new middleware.
import assert from 'assert'
import createServer, { buffer, 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('/', buffer(), async (request: IHttpRequest, response: IHttpResponse) => {
assert.strictEqual(Buffer.isBuffer(request.body), true)
})
// maximum input size: 256 MB
app.put('/', buffer(256), async (request: IHttpRequest, response: IHttpResponse) => {
assert.strictEqual(Buffer.isBuffer(request.body), true)
})
// maximum input size: 384 MB
app.patch('/', buffer({ limit: 402653184 }), async (request: IHttpRequest, response: IHttpResponse) => {
assert.strictEqual(Buffer.isBuffer(request.body), true)
})
app.delete('/', buffer({ limit: 1048576, onLimitReached: handleLimitReached }), async (request: IHttpRequest, response: IHttpResponse) => {
// alternative:
// app.delete('/', buffer(1, handleLimitReached), async (request: IHttpRequest, response: IHttpResponse) => {
assert.strictEqual(Buffer.isBuffer(request.body), true)
})
Optional
onLimitReached: Nilable<HttpRequestHandler>Generated using TypeDoc
Creates a middleware, that reads the whole input of the request stream and writes data to 'body' property of the request context as buffer.