Options
All
  • Public
  • Public/Protected
  • All
Menu

Module http

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
},
}
},
],
});

Index

Other

DefaultRequestRoute: { default: true; handler: RequestHandler; name?: string }

Type declaration

DefaultUpgradeRoute: { default: true; handler: UpgradeHandler; name?: string }

Type declaration

HttpConfigs: { listenOptions?: ServerListenOptions; noServer?: boolean }

Type declaration

  • Optional listenOptions?: ServerListenOptions

    The options passed to http.Server.listen() method

  • Optional noServer?: boolean

    Set to true to stop HTTP server listening when app start

HttpRequestInfo: { body?: string; headers: IncomingHttpHeaders; method: string; url: string }

Type declaration

  • Optional body?: string
  • headers: IncomingHttpHeaders
  • method: string
  • url: string
RequestHandler: (req: IncomingMessage, res: ServerResponse, routingInfo: RoutingInfo) => void

Type declaration

    • (req: IncomingMessage, res: ServerResponse, routingInfo: RoutingInfo): void
    • Parameters

      • req: IncomingMessage
      • res: ServerResponse
      • routingInfo: RoutingInfo

      Returns void

RequestRoute: { default?: false; handler: RequestHandler; name?: string; path: string }

Type declaration

  • Optional default?: false
  • handler: RequestHandler
  • Optional name?: string
  • path: string
RoutingInfo: { matchedPath?: string; originalPath: string; trailingPath: string }

Type declaration

  • Optional matchedPath?: string
  • originalPath: string
  • trailingPath: string
ServerListenOptions: ListenOptions
UpgradeHandler: (req: IncomingMessage, socket: Socket, head: Buffer, routingInfo: RoutingInfo) => void

Type declaration

    • (req: IncomingMessage, socket: Socket, head: Buffer, routingInfo: RoutingInfo): void
    • Parameters

      • req: IncomingMessage
      • socket: Socket
      • head: Buffer
      • routingInfo: RoutingInfo

      Returns void

UpgradeRoute: { default?: false; handler: UpgradeHandler; name?: string; path: string }

Type declaration

  • Optional default?: false
  • handler: UpgradeHandler
  • Optional name?: string
  • path: string

Generated using TypeDoc