Socket
Book a DemoInstallSign in
Socket

@useoptic/hapi-middleware

Package Overview
Dependencies
Maintainers
5
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@useoptic/hapi-middleware

NodeJS middleware for using Optic within Hapi

latest
Source
npmnpm
Version
0.0.6
Version published
Maintainers
5
Created
Source

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

FAQs

Package last updated on 20 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts