0.0.6 • Published 3 years ago

@useoptic/hapi-middleware v0.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

@useoptic/hapi-middleware

NPM GitHub Workflow Status

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 to false) Programmatically control if capturing data and sending it to Optic
  • uploadUrl: string (defaults to process.env.OPTIC_LOGGING_URL) The URL to Optics capture URL, if left blank it will expect OPTIC_LOGGING_URL environment variable set by the Optic CLI
  • console: boolean (defaults to false) 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.

0.0.5

3 years ago

0.0.6

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago