Socket
Book a DemoInstallSign in
Socket

@machinat/http

Package Overview
Dependencies
Maintainers
2
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

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.

latest
Source
npmnpm
Version
0.6.0
Version published
Maintainers
2
Created
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

Package last updated on 08 Jun 2022

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