Socket
Socket
Sign inDemoInstall

@machinat/http

Package Overview
Dependencies
20
Maintainers
2
Versions
26
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @machinat/http

This module provide HTTP listening capability to all the services require it. For example, to serve a webhook for subscribing events from chat platform.


Version published
Weekly downloads
14
Maintainers
2
Install size
548 kB
Created
Weekly downloads
 

Readme

Source

Http Module

This module provide HTTP listening capability to all the services require it. For example, to serve a webhook for subscribing events from chat platform.

Install

npm install @machinat/core @machinat/http
# or with yarn
yarn add @machinat/core @machinat/http

Docs

Check the package reference.

Setup

Use listenOptions to set the network options for server.listen().

import Machinat from '@machinat/core';
import Http from '@machinat/http';

const app = Machinat.createApp({
  modules: [
    Http.initModule({
      listenOptions: {
        port: 8080,
        host: 'localhost',
      }
    }),
  ],
});

Usage

Provide Http.RequestRouteList or Http.UpgradeRouteList (for WebSocket) to register RPC style routes. All the requests under the registered path will be received by the handler. You can also use default: true to catch all unmatch requests.

const app = Machinat.createApp({
  modules: [
    Http.initModule({ /* ... */ }),
  ],
  service: [
    { // route listening to /api
      provide: Http.RequestRouteList,
      withValue: {
        name: 'my_api',
        path: '/api',
        handler: (req, res) => {
          // handle http requests
        },
      }
    },
    { // default request route
      provide: Http.RequestRouteList,
      withValue: {
        name: 'default',
        default: true,
        handler: (req, res) => {
          // catch requests not matching any route
        },
      }
    },
    {  // handle WebSocket connection
      provide: Http.UpgradeRouteList,
      withValue: {
        name: 'web_socket',
        path: '/',
        handler: (req, head, socket) => {
          // handle http upgrade requests
        },
      }
    },
  ],
});

FAQs

Last updated on 08 Jun 2022

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc