0.8.0 • Published 2 years ago

@sociably/http v0.8.0

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

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 @sociably/core @sociably/http
# or with yarn
yarn add @sociably/core @sociably/http

Docs

Check the package reference.

Setup

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

import Sociably from '@sociably/core';
import Http from '@sociably/http';

const app = Sociably.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 = Sociably.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
        },
      }
    },
  ],
});
0.9.0-alpha.0

2 years ago

0.9.0-alpha.3

2 years ago

0.9.0-alpha.4

2 years ago

0.9.0-alpha.26

2 years ago

0.8.0

2 years ago

0.8.0-alpha.28

2 years ago

0.8.0-alpha.10

2 years ago

0.8.0-alpha.8

2 years ago

0.8.0-alpha.0

3 years ago

0.8.0-alpha.1

3 years ago

0.7.1

3 years ago

0.7.0

3 years ago