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