2.1.0 • Published 1 year ago

@mangar2/httpservice v2.1.0

Weekly downloads
5
License
LGPL-3.0-or-later
Repository
github
Last release
1 year ago

Abstract

Provides simple http/https client and server classes

Contents

Meta

Filehttpservice.js
AbstractProvides simple http/https client and server classes
AuthorVolker Böhm
CopyrightCopyright ( c ) 2020 Volker Böhm
LicenseThis 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

NameTypeDescription
paramsURLSearchParamsSearch parameter of the GET URL
headersObjectheaders
pathstringpath name ( without search parameters )
resObjecthttp ( s ) res structure

HTTPCallback

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

HTTPCallback Parameters

NameTypeDescription
payloadstringhttp payload ( maybe stringified JSON )
headersObjectheaders
pathstringpath name ( without search parameters )
resObjecthttp ( s ) res structure

Global functions

echo

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

Handles a post request

echo Parameters

NameTypeDescription
methodstringmethod name
headerobjectheader of the sender
payloadstringobject payload
pathstringpath of the request
resobjectresult 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

NameTypeAttributeDefaultDescription
hoststringhost name or ip address
portstring, numberport number
typestringoptional'http'either 'http' or 'https' for http/https clients

HTTPClient Members

NameTypedescription
agent | @description

HTTPClient Methods

close

close () => {promise}

Aborts all open requests

close returns
TypeDescription
promise, resolved once all connections are closed

getRequest

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

Sends a get request

getRequest Parameters
NameTypeAttributeDefaultDescription
pathstringhttp path
headersobjectoptional{ }header to send
getRequest returns
TypeDescription
Promise<{statusCode, headers, payload> } Promise; resolve = { statusCode , headers , payload }

post

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

Sends a post request

post Parameters
NameTypeAttributeDescription
sendOptionsobjectall information required for sending
typestringoptionaltype of the payload data : html , text , json , form , xml

sendOptions properties

NameTypeAttributeDefaultDescription
pathstringhttp path
payloadobjectoptionalpayload to send
headersobjectoptional{ }header to send
post returns
TypeDescription
Promise<{statusCode, headers, payload> } Promise; resolve = { statusCode , headers , payload }

put

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

Sends a put request

put Parameters
NameTypeAttributeDescription
sendOptionsobjectall information required for sending
typestringoptionaltype of the payload data : html , text , json , form , xml

sendOptions properties

NameTypeAttributeDefaultDescription
pathstringhttp path
payloadobjectoptionalpayload to send
headersobjectoptional{ }header to send
put returns
TypeDescription
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
NameTypeAttributeDefaultDescription
pathstringhttp path
methodstringhttp send method PUT , GET , . . .
payloadstring, objectpayload to send
headersobjectoptional{ }header to send
send returns
TypeDescription
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
NameTypeAttributeDescription
sendOptionsobjectall information required for sending
typestringoptionaltype of the payload data : html , text , json , form , xml

sendOptions properties

NameTypeAttributeDefaultDescription
pathstringhttp path
methodstringhttp send method PUT , GET , . . .
payloadobjectoptionalpayload to send
headersobjectoptional{ }header to send
sendv2 returns
TypeDescription
Promise<{statusCode, headers, payload> } Promise; resolve = { statusCode , headers , payload }

setConnection

setConnection (host, port)

Sets host name and port number

setConnection Parameters
NameTypeDescription
hoststringhost name
portstringport 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

NameTypeDescription
hoststringhost name or ip address
portstring, numberport number

HTTPSClient Methods

close

close () => {promise}

Aborts all open requests

close returns
TypeDescription
promiseresolved once all connections are closed

getRequest

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

Sends a get request

getRequest Parameters
NameTypeAttributeDefaultDescription
pathstringhttp path
headersobjectoptional{ }header to send
getRequest returns
TypeDescription
Promise<{statusCode, headers, payload> } Promise; resolve = { statusCode , headers , payload }

post

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

Sends a post request

post Parameters
NameTypeAttributeDescription
sendOptionsobjectall information required for sending
typestringoptionaltype of the payload data : html , text , json , form , xml

sendOptions properties

NameTypeAttributeDefaultDescription
pathstringhttp path
payloadobjectoptionalpayload to send
headersobjectoptional{ }header to send
post returns
TypeDescription
Promise<{statusCode, headers, payload> } Promise; resolve = { statusCode , headers , payload }

put

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

Sends a put request

put Parameters
NameTypeAttributeDescription
sendOptionsobjectall information required for sending
typestringoptionaltype of the payload data : html , text , json , form , xml

sendOptions properties

NameTypeAttributeDefaultDescription
pathstringhttp path
payloadobjectoptionalpayload to send
headersobjectoptional{ }header to send
put returns
TypeDescription
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
NameTypeAttributeDefaultDescription
pathstringhttp path
methodstringhttp send method PUT , GET , . . .
payloadstring, objectpayload to send
headersobjectoptional{ }header to send
send returns
TypeDescription
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
NameTypeAttributeDescription
sendOptionsobjectall information required for sending
typestringoptionaltype of the payload data : html , text , json , form , xml

sendOptions properties

NameTypeAttributeDefaultDescription
pathstringhttp path
methodstringhttp send method PUT , GET , . . .
payloadobjectoptionalpayload to send
headersobjectoptional{ }header to send
sendv2 returns
TypeDescription
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

NameTypeDescription
portnumberport to listen to

HTTPSServer Members

NameTypedescription
address@typeGets 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
TypeDescription
promiseresolved , once the connection is closed

listen

listen (options)

Creates a https server listening

listen Parameters
NameTypeDescription
optionsObjectadditional options

on

on (event, callback)

Sets a callback

on Parameters
NameTypeDescription
eventstringRESTful http verb ( POST , GET , PUT , PATCH , DELETE , OPTIONS ) and 'listen' ( called once listening ) and 'closed' ( called once closed )
callbackHTTPGetCallback, 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

NameTypeDescription
portnumberport to listen to

HTTPServer Members

NameTypedescription
address@typeGets 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
TypeDescription
promiseresolved , once the connection is closed

listen

listen ()

Creates a http server listening

on

on (event, callback)

Sets a callback

on Parameters
NameTypeDescription
eventstringRESTful http verb ( POST , GET , PUT , PATCH , DELETE , OPTIONS ) and 'listen' ( called once listening ) and 'closed' ( called once closed )
callbackHTTPGetCallback, 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