• 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.

    Returns UniqueHttpMiddleware

    The new middleware.

    Example

    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')
    })
  • Parameters

    Returns UniqueHttpMiddleware

  • Parameters

    Returns UniqueHttpMiddleware

Generated using TypeDoc