Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@hapi/hapi

Package Overview
Dependencies
Maintainers
7
Versions
50
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hapi/hapi

HTTP Server framework

  • 21.3.12
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
820K
increased by5.86%
Maintainers
7
Weekly downloads
 
Created

What is @hapi/hapi?

@hapi/hapi is a rich framework for building applications and services in Node.js. It is known for its powerful plugin system, configuration-based approach, and focus on security and performance.

What are @hapi/hapi's main functionalities?

Routing

This code demonstrates how to set up a basic HTTP server with a single route using @hapi/hapi. The server listens on port 3000 and responds with 'Hello, world!' when the root URL is accessed.

const Hapi = require('@hapi/hapi');

const init = async () => {
  const server = Hapi.server({
    port: 3000,
    host: 'localhost'
  });

  server.route({
    method: 'GET',
    path: '/',
    handler: (request, h) => {
      return 'Hello, world!';
    }
  });

  await server.start();
  console.log('Server running on %s', server.info.uri);
};

process.on('unhandledRejection', (err) => {
  console.log(err);
  process.exit(1);
});

init();

Plugins

This code demonstrates how to create and register a plugin in @hapi/hapi. The plugin adds a new route '/plugin' that responds with 'Hello from plugin!'.

const Hapi = require('@hapi/hapi');

const init = async () => {
  const server = Hapi.server({
    port: 3000,
    host: 'localhost'
  });

  const plugin = {
    name: 'myPlugin',
    version: '1.0.0',
    register: async function (server, options) {
      server.route({
        method: 'GET',
        path: '/plugin',
        handler: (request, h) => {
          return 'Hello from plugin!';
        }
      });
    }
  };

  await server.register(plugin);
  await server.start();
  console.log('Server running on %s', server.info.uri);
};

process.on('unhandledRejection', (err) => {
  console.log(err);
  process.exit(1);
});

init();

Validation

This code demonstrates how to use Joi for payload validation in @hapi/hapi. The route '/data' expects a POST request with a payload containing a 'name' and 'age' field, both of which are validated according to the specified rules.

const Hapi = require('@hapi/hapi');
const Joi = require('joi');

const init = async () => {
  const server = Hapi.server({
    port: 3000,
    host: 'localhost'
  });

  server.route({
    method: 'POST',
    path: '/data',
    options: {
      validate: {
        payload: Joi.object({
          name: Joi.string().min(3).max(30).required(),
          age: Joi.number().integer().min(0).required()
        })
      }
    },
    handler: (request, h) => {
      return `Hello, ${request.payload.name}!`;
    }
  });

  await server.start();
  console.log('Server running on %s', server.info.uri);
};

process.on('unhandledRejection', (err) => {
  console.log(err);
  process.exit(1);
});

init();

Other packages similar to @hapi/hapi

Keywords

FAQs

Package last updated on 24 Oct 2024

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc