npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mangar2/httpservice

v2.1.0

Published

implements a simple http/https client/server

Downloads

5

Readme

Abstract

Provides simple http/https client and server classes

Contents

Meta

| | | | --- | --- | | File | httpservice.js | | Abstract | Provides simple http/https client and server classes | | Author | Volker Böhm | | Copyright | Copyright ( c ) 2020 Volker Böhm | | License | This software is licensed under the GNU LESSER GENERAL PUBLIC LICENSE Version 3 . It is furnished "as is" , without any support , and with no warranty , express or implied , as to its usefulness for any purpose . |

Callback definitions

HTTPGetCallback

Callback for HTTP ( S ) GET requests

HTTPGetCallback Parameters

| Name | Type | Description | | ---------- | ------------ | ----------------- | | params | URLSearchParams | Search parameter of the GET URL | | | headers | Object | headers | | | path | string | path name ( without search parameters ) | | | res | Object | http ( s ) res structure | |

HTTPCallback

Callback for HTTP ( S ) put , post , patch , delete , listen , or closed requests

HTTPCallback Parameters

| Name | Type | Description | | ---------- | ------------ | ----------------- | | payload | string | http payload ( maybe stringified JSON ) | | | headers | Object | headers | | | path | string | path name ( without search parameters ) | | | res | Object | http ( s ) res structure | |

Global functions

echo

async echo (method, header, payload, path, res)

Handles a post request

echo Parameters

| Name | Type | Description | | ---------- | ------------ | ----------------- | | method | string | method name | | | header | object | header of the sender | | | payload | string | object payload | | | path | string | path of the request | | | res | object | result object | |

run

run ()

Starts the server

Class HTTPClient

new HTTPClient(host, port, type)

Class simplifying access to the node http service for http clients

Example

const client = new HTTPClient('myhost', 10000);
client.sendv2({ path: 'info/getdata/1', method: 'GET' })
client.post({ path: 'postdata/1', payload={info: 'hello world')}, type='json' })
client.get('info/getdata/1')

HTTPClient Parameters

| Name | Type | Attribute | Default | Description | | ---------- | ------------ | ------------ | ------------ | ----------------- | | host | string | | | host name or ip address | | | port | string, number | | | port number | | | type | string | optional | 'http' | either 'http' or 'https' for http/https clients | |

HTTPClient Members

| Name | Type | description | | ------------ | ------------ | ------------ | | agent | `` | @description |

HTTPClient Methods

close

close () => {promise}

Aborts all open requests

close returns

| Type | Description | | ---- | ----------- | | promise | , resolved once all connections are closed |

getRequest

getRequest (path, headers) => {Promise<{statusCode, headers, payload}

Sends a get request

getRequest Parameters

| Name | Type | Attribute | Default | Description | | ---------- | ------------ | ------------ | ------------ | ----------------- | | path | string | | | http path | | | headers | object | optional | { } | header to send | |

getRequest returns

| Type | Description | | ---- | ----------- | | Promise<{statusCode, headers, payload | > } Promise; resolve = { statusCode , headers , payload } |

post

post (sendOptions, type) => {Promise<{statusCode, headers, payload}

Sends a post request

post Parameters

| Name | Type | Attribute | Description | | ---------- | ------------ | ------------ | ----------------- | | sendOptions | object | | all information required for sending | | | type | string | optional | type of the payload data : html , text , json , form , xml | |

sendOptions properties

| Name | Type | Attribute | Default | Description | | ---------- | ------------ | ------------ | ------------ | ----------------- | | path | string | | | http path | | | payload | object | optional | | payload to send | | | headers | object | optional | { } | header to send | |

post returns

| Type | Description | | ---- | ----------- | | Promise<{statusCode, headers, payload | > } Promise; resolve = { statusCode , headers , payload } |

put

put (sendOptions, type) => {Promise<{statusCode, headers, payload}

Sends a put request

put Parameters

| Name | Type | Attribute | Description | | ---------- | ------------ | ------------ | ----------------- | | sendOptions | object | | all information required for sending | | | type | string | optional | type of the payload data : html , text , json , form , xml | |

sendOptions properties

| Name | Type | Attribute | Default | Description | | ---------- | ------------ | ------------ | ------------ | ----------------- | | path | string | | | http path | | | payload | object | optional | | payload to send | | | headers | object | optional | { } | header to send | |

put returns

| Type | Description | | ---- | ----------- | | Promise<{statusCode, headers, payload | > } Promise; resolve = { statusCode , headers , payload } |

send

send (path, method, payload, headers) => {Promise<{statusCode, headers, payload}

Sends data . A payload of type "object" is automatically stringified , a string is not

send Parameters

| Name | Type | Attribute | Default | Description | | ---------- | ------------ | ------------ | ------------ | ----------------- | | path | string | | | http path | | | method | string | | | http send method PUT , GET , . . . | | | payload | string, object | | | payload to send | | | headers | object | optional | { } | header to send | |

send returns

| Type | Description | | ---- | ----------- | | Promise<{statusCode, headers, payload | > } Promise; resolve = { statusCode , headers , payload } |

sendv2

sendv2 (sendOptions, type) => {Promise<{statusCode, headers, payload}

Sends data . Adds a content-type element to the header and stringifies the boy based on the type

sendv2 Parameters

| Name | Type | Attribute | Description | | ---------- | ------------ | ------------ | ----------------- | | sendOptions | object | | all information required for sending | | | type | string | optional | type of the payload data : html , text , json , form , xml | |

sendOptions properties

| Name | Type | Attribute | Default | Description | | ---------- | ------------ | ------------ | ------------ | ----------------- | | path | string | | | http path | | | method | string | | | http send method PUT , GET , . . . | | | payload | object | optional | | payload to send | | | headers | object | optional | { } | header to send | |

sendv2 returns

| Type | Description | | ---- | ----------- | | Promise<{statusCode, headers, payload | > } Promise; resolve = { statusCode , headers , payload } |

setConnection

setConnection (host, port)

Sets host name and port number

setConnection Parameters

| Name | Type | Description | | ---------- | ------------ | ----------------- | | host | string | host name | | | port | string | port number | |

Class HTTPSClient

new HTTPSClient(host, port)

Class simplifying access to the node http service for http clients

Example

client.sendv2({ path: 'info/getdata/1', method: 'GET' })
client.post({ path: 'postdata/1', payload={info: 'hello world')}, type='json' })
client.get('info/getdata/1')

HTTPSClient Parameters

| Name | Type | Description | | ---------- | ------------ | ----------------- | | host | string | host name or ip address | | | port | string, number | port number | |

HTTPSClient Methods

close

close () => {promise}

Aborts all open requests

close returns

| Type | Description | | ---- | ----------- | | promise | resolved once all connections are closed |

getRequest

getRequest (path, headers) => {Promise<{statusCode, headers, payload}

Sends a get request

getRequest Parameters

| Name | Type | Attribute | Default | Description | | ---------- | ------------ | ------------ | ------------ | ----------------- | | path | string | | | http path | | | headers | object | optional | { } | header to send | |

getRequest returns

| Type | Description | | ---- | ----------- | | Promise<{statusCode, headers, payload | > } Promise; resolve = { statusCode , headers , payload } |

post

post (sendOptions, type) => {Promise<{statusCode, headers, payload}

Sends a post request

post Parameters

| Name | Type | Attribute | Description | | ---------- | ------------ | ------------ | ----------------- | | sendOptions | object | | all information required for sending | | | type | string | optional | type of the payload data : html , text , json , form , xml | |

sendOptions properties

| Name | Type | Attribute | Default | Description | | ---------- | ------------ | ------------ | ------------ | ----------------- | | path | string | | | http path | | | payload | object | optional | | payload to send | | | headers | object | optional | { } | header to send | |

post returns

| Type | Description | | ---- | ----------- | | Promise<{statusCode, headers, payload | > } Promise; resolve = { statusCode , headers , payload } |

put

put (sendOptions, type) => {Promise<{statusCode, headers, payload}

Sends a put request

put Parameters

| Name | Type | Attribute | Description | | ---------- | ------------ | ------------ | ----------------- | | sendOptions | object | | all information required for sending | | | type | string | optional | type of the payload data : html , text , json , form , xml | |

sendOptions properties

| Name | Type | Attribute | Default | Description | | ---------- | ------------ | ------------ | ------------ | ----------------- | | path | string | | | http path | | | payload | object | optional | | payload to send | | | headers | object | optional | { } | header to send | |

put returns

| Type | Description | | ---- | ----------- | | Promise<{statusCode, headers, payload | > } Promise; resolve = { statusCode , headers , payload } |

send

send (path, method, payload, headers) => {Promise<{statusCode, headers, payload}

Sends data . A payload of type "object" is automatically stringified , a string is not

send Parameters

| Name | Type | Attribute | Default | Description | | ---------- | ------------ | ------------ | ------------ | ----------------- | | path | string | | | http path | | | method | string | | | http send method PUT , GET , . . . | | | payload | string, object | | | payload to send | | | headers | object | optional | { } | header to send | |

send returns

| Type | Description | | ---- | ----------- | | Promise<{statusCode, headers, payload | > } Promise; resolve = { statusCode , headers , payload } |

sendv2

sendv2 (sendOptions, type) => {Promise<{statusCode, headers, payload}

Sends data . Adds a content-type element to the header and stringifies the boy based on the type

sendv2 Parameters

| Name | Type | Attribute | Description | | ---------- | ------------ | ------------ | ----------------- | | sendOptions | object | | all information required for sending | | | type | string | optional | type of the payload data : html , text , json , form , xml | |

sendOptions properties

| Name | Type | Attribute | Default | Description | | ---------- | ------------ | ------------ | ------------ | ----------------- | | path | string | | | http path | | | method | string | | | http send method PUT , GET , . . . | | | payload | object | optional | | payload to send | | | headers | object | optional | { } | header to send | |

sendv2 returns

| Type | Description | | ---- | ----------- | | Promise<{statusCode, headers, payload | > } Promise; resolve = { statusCode , headers , payload } |

Class HTTPSServer

new HTTPSServer(port)

Creates a http server listening to a certain port for simplifying access to the node http service Register your callbacks according to the http functions you need .

Example

const server = new HTTPSServer(10000)
server.on('GET', (URLSearchParam, headers, path, res) => console.log(path))
const httpsOptions = {
  key: fs.readFileSync('key.pem'),
  cert: fs.readFileSync('cert.pem')
}
server.listen(httpsOptions)
server.close()

HTTPSServer Parameters

| Name | Type | Description | | ---------- | ------------ | ----------------- | | port | number | port to listen to | |

HTTPSServer Members

| Name | Type | description | | ------------ | ------------ | ------------ | | address | @type | Gets the address of the server , once listen has been called If listen has not been called yet , undefined is returned |

HTTPSServer Methods

close

close () => {promise}

Stops the server from accepting new connections and closes existing connections . Calls the callback "close" , once the server is closed .

close returns

| Type | Description | | ---- | ----------- | | promise | resolved , once the connection is closed |

listen

listen (options)

Creates a https server listening

listen Parameters

| Name | Type | Description | | ---------- | ------------ | ----------------- | | options | Object | additional options | |

on

on (event, callback)

Sets a callback

on Parameters

| Name | Type | Description | | ---------- | ------------ | ----------------- | | event | string | RESTful http verb ( POST , GET , PUT , PATCH , DELETE , OPTIONS ) and 'listen' ( called once listening ) and 'closed' ( called once closed ) | | | callback | HTTPGetCallback, HTTPCallback | ( payload , headers , path , res ) where payload is the http payload , headers the http headers , res the result structure and path is is the http path as string | |

Class HTTPServer

new HTTPServer(port)

Creates a http server listening to a certain port for simplifying access to the node http service Register your callbacks according to the http functions you need .

Example

const server = new HTTPServer(10000)
server.on('GET', (URLSearchParam, headers, path, res) => console.log(path))
server.listen()
server.close()

HTTPServer Parameters

| Name | Type | Description | | ---------- | ------------ | ----------------- | | port | number | port to listen to | |

HTTPServer Members

| Name | Type | description | | ------------ | ------------ | ------------ | | address | @type | Gets the address of the server , once listen has been called If listen has not been called yet , undefined is returned |

HTTPServer Methods

close

close () => {promise}

Stops the server from accepting new connections and closes existing connections . Calls the callback "close" , once the server is closed .

close returns

| Type | Description | | ---- | ----------- | | promise | resolved , once the connection is closed |

listen

listen ()

Creates a http server listening

on

on (event, callback)

Sets a callback

on Parameters

| Name | Type | Description | | ---------- | ------------ | ----------------- | | event | string | RESTful http verb ( POST , GET , PUT , PATCH , DELETE , OPTIONS ) and 'listen' ( called once listening ) and 'closed' ( called once closed ) | | | callback | HTTPGetCallback, HTTPCallback | ( payload , headers , path , res ) where payload is the http payload , headers the http headers , res the result structure and path is is the http path as string | |