Mppx.toNodeListener
Adapt payments for Node.js HTTP
Wraps a payment handler to create a Node.js HTTP listener.
Usage
import * as from 'node:http'
import { , } from 'mppx/server'
const = .({
: [.()],
})
.(async (, ) => {
const = await .(
.({
: '0.1',
: '0x20c0000000000000000000006a37DA5C996874BE', // OUSD on Tempo
: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
}),
)(, )
if (. === 402) return
.('OK')
})Behavior
- On an oversized body: Writes
413 Payload Too Large. Request bodies default to a 10 MiB limit. - On
402: Writes the Challenge response headers and body, then ends the connection. - On
200: Sets thePayment-Receiptheader; the caller writes the response body.
Return type
type ReturnType = (
req: IncomingMessage,
res: ServerResponse,
) => Promise<MethodFn.Response<Transport.Http>>Parameters
handler
- Type:
(input: Request) => Promise<MethodFn.Response<Transport.Http>>
The payment handler function returned by calling an intent on the payment object.
options (optional)
- Type:
{ host?: string; maxBodySize?: number; protocol?: string }
Configures Node-to-Fetch request conversion. maxBodySize defaults to 10_485_760 bytes (10 MiB).
import * as from 'node:http'
import { , } from 'mppx/server'
const = .({
: [.()],
})
.(async (, ) => {
const = await .(
.({
: '0.1',
: '0x20c0000000000000000000006a37DA5C996874BE',
: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
}),
)(, )
if (. === 402) return
.('OK')
})