@useoptic/hapi-middleware
This module is an hapi plugin using @useoptic/optic-node-sdk to capture and format HTTP data to send to Optic. We have a list of middleware available for some frameworks, if we are missing the framework join our community and suggest the next framework or develop it with us.
Requirements
The module requires @useoptic/cli
to be installed, instructions on installing it are available https://www.useoptic.com/docs/.
Install
npm install @useoptic/hapi-middleware
Usage
The middleware takes a configuration object and captures traffic in the background as long as @useoptic/cli
is installed.
Configuration
All options are optional for easier configuration in your application
-
enabled
:boolean
(defaults tofalse
) Programmatically control if capturing data and sending it to Optic -
uploadUrl
:string
(defaults toprocess.env.OPTIC_LOGGING_URL
) The URL to Optics capture URL, if left blank it will expectOPTIC_LOGGING_URL
environment variable set by the Optic CLI -
console
:boolean
(defaults tofalse
) Send to stdout/console for debugging -
framework
:string
(defaults to '') Additional information to inform Optic of where it is capturing information
Example
Using a basic hapi server.
const Hapi = require('@hapi/hapi')
const { OpticPlugin } = require('@useoptic/hapi-middleware')
const init = async () => {
const server = Hapi.server({
port: 3001,
host: 'localhost'
})
await server.register({
plugin: OpticPlugin,
options: {
enabled: true
}
})
server.route({
method: ['GET', 'POST'],
path: '/',
handler: (request, h) => {
return 'Hello World! ' + Math.random()
}
})
await server.start()
console.log('Server running on %s', server.info.uri)
}
process.on('unhandledRejection', (err) => {
if (err) {
console.log('ERROR')
console.log(err)
process.exit(1)
}
})
init()
To start capturing data from the SDK, run your application with
api exec "node <your hapi server>"
License
This software is licensed under the MIT license.