@spicadev/network

0.3.0-tr3 • Public • Published

@spicadev/network NPM

Simplify networking in Node.js with support for TLS, UDP, WS, Socket, DNS, and TCP.

Installation

Via npm:

npm install @spicadev/network

Via Node.js:

const net = require('@spicadev/network')
const { http } = require('@spicadev/network')

Using import:

import net from '@spicadev/network'
import * as net from '@spicadev/network'
import { http } from '@spicadev/network'

Changes

  • Modified README.md
  • Bug Fix + More efficient
  • Added redirect property to HttpClientOptions
  • Removed Herobrine

Next Update (Plan)

  • Socket APIs
  • WebSocket APIs

API

net.http.Server(options?: HttpServerOptions)

Create a http server instance.

net.http.Client(...args?: HttpClientRequestArguments)

Create a http client instance.

net.http.ClientRequest(...HttpClientRequestArguments):

Promise<net.http.ClientResponse, Error>?

Create a http request

net.http.ClientResponse(

res: http.IncomingMessage | http2.ClientResponse,

data: Buffer)

Create a http response

net.http.http

Object containing http: node:http, https: node:https, http2: node:http2 | null, and get(options?: HttpChooserOptions)

Static Methods

net.http.Server.createServer(options?: HttpServerOptions): http.Server

Alias for http.Server constructor

net.http.Client.createClient(...HttpClientRequestArguments):

Promise<net.http.ClientResponse, Error>?

Alias for http.Client constructor

Prototypes

net.http.Server.route(httpMethod: string, route: string | regex,

callback: function(req, res, next)): net.http.Server

Open a route with specified http method, route/path, and callback

net.http.Server[HttpMethods](route: string | regex, callback: function(req, res, next)): net.http.Server

Open a route with specified http method, route/path, and callback (if http-method haven't implemented, use net.http.Server.route instead)

net.http.Server.hook(name: HttpHooks, callback: function): http.Server

Hook the available hooks

net.http.Server.decorate(name: string, value: any): http.Server

Decorate the server object, and req+res object per request

net.http.Server.on(event: string, callback: Function): void

Listen to event emitted by EventEmitter

Available events:

  • error
  • clientError
net.http.Server.listen(): Promise<ServerAddress, void>

Listen to the server

net.http.Server.close(): Promise<void, void>

Close the server

net.http.Client.request(...HttpClientRequestArguments): Promise<net.http.ClientResponse, Error>

Create a request

net.http.Client[HttpMethods](...HttpClientRequestArguments): Promise<net.http.ClientResponse, Error>

Create a request with specific http-method (Use http.Client.request for more flexibility)

net.http.http.get(options?: HttpChooserOptions): node:http | node:https | node:http2

Retrieve nodejs http(s) module

Types

Header

  • Object

HttpHooks

Hook 1st argument are always req: net.http.ServerRequest

Hook 2nd argument are always res: net.http.ServerResponse

  • requestIncoming(...HooksArguments)
  • preProcess(...HooksArguments)
  • error(...HooksArguments, error: Error)
  • unhandledRoute(...HooksArguments)

HttpMethods

  • get
  • post
  • put
  • patch
  • delete

HttpServerOptions

  • http -> HTTP-related options
    • ...all node:http.createServer options except IncomingMessage and ServerResponse
    • ...all node:http2.createServer options excopt IncomingMessage and ServerResponse
  • secure -> HTTP2 and HTTPS related options
    • cert: string -> Filepath targeting your ssl certificate.
    • key: string -> Filepath targeting your ssl key
  • ...ServerAddress -> Server address (excluding family)
  • ...HttpChooserOptions -> Chooser options

HttpClientOptions

  • version: number -> The http version to be use, Either 1 (http+https) or 2 (http2), default: 1
  • url: ValidURL -> URL for the request to be send to
  • body: ValidBody -> The request body, default: null
  • headers: Headers -> The request header, default: null
  • method: HTTPMethod -> HTTP method to use, default: GET
  • redirect: string -> What to do when there are redirect, either follow, manual, and error

HttpClientRequestArguments

  • 2 Arguments:
    • url: ValidURL -> URL for the request to be send to, can be placed both as 1st or 2nd argument
    • options: HttpClientOptions -> Options used, can be placed both as 1st or 2nd argument
  • 1 Argument: ValidURL | HttpClientOptions -> Either the url, or options that contain url property.

HttpChooserOptions

  • useHTTP2: boolean -> Wheter to create http2 (secure) server, throw error of http2 doesn't available, default: false
  • secure: boolean -> Wheter to create a https (secure) server, default: false

ServerAddress

  • host: string -> The server host, default for server: 0.0.0.0
  • port: number -> The server port, default for server: 3000
  • family: boolean -> Either Ipv4 or Ipv6

ValidURL

  • Native URL
  • net.utilty.Url
  • string
  • object with the same property as URL (for example: host, protocol, pathname)

ValidBody

  • Object (json-stringified)
  • String
  • Buffer
  • ArrayBuffer
  • All typed array including Uint8Array

Dependencies (1)

Dev Dependencies (0)

    Package Sidebar

    Install

    npm i @spicadev/network

    Weekly Downloads

    1

    Version

    0.3.0-tr3

    License

    MIT

    Unpacked Size

    525 kB

    Total Files

    22

    Last publish

    Collaborators

    • spicadev