bare-https
HTTPS library for JavaScript
bare-https — HTTPS library for JavaScript.
Mirrors the Node.js https module.
npm i bare-httpsUsage
const https = require('bare-https')
const options = {
cert: fs.readFileSync('test/fixtures/cert.crt'),
key: fs.readFileSync('test/fixtures/cert.key')
}
const server = https.createServer(options, (req, res) => {
res.statusCode = 200
res.setHeader('Content-Length', 10)
res.write('hello world!')
res.end()
})
server.listen(0, () => {
const { port } = server.address()
console.log('server is bound on', port)
const client = https.request({ port }, (res) => {
res.on('data', (data) => console.log(data.toString()))
})
client.end()
})API
HTTPSAgent
createConnection(opts?: HTTPSSocketOptions): HTTPSSocket
Source
Creates a new HTTPSSocket connection wrapping a plain TCP connection in TLS.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
opts? | HTTPSSocketOptions | — | Options for the underlying TCP connection and its TLS wrapper. |
HTTPSAgent.global: HTTPSAgent
Source
The agent's own default instance (created with keepAlive: 1000 and timeout: 5000), used as bare-https's globalAgent.
HTTPSServer
HTTPSServer
new HTTPSServer(opts?: HTTPSServerOptions, onrequest?: (req: HTTPIncomingMessage, res: HTTPServerResponse) => void)An HTTPS server, reusing bare-http1's request parsing and response handling over HTTPSSocket connections instead of plain TCPSocket connections.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
opts? | HTTPSServerOptions | — | Server options: TLS socket options (e.g. cert, key) plus bare-http1 server connection options. |
onrequest? | (req: HTTPIncomingMessage, res: HTTPServerResponse) => void | — | Added as a 'request' listener. |
HTTPSClientRequest
new HTTPSClientRequest(opts?: HTTPSClientRequestOptions, onresponse?: () => void)
Source
An outgoing HTTPS request, extending bare-http1's HTTPClientRequest but defaulting to an HTTPSAgent instead of an HTTPAgent.
Overloads:
new HTTPSClientRequest(opts?: HTTPSClientRequestOptions, onresponse?: () => void)
new HTTPSClientRequest(onresponse: () => void)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
opts? | HTTPSClientRequestOptions | — | bare-http1 client request options; agent defaults to HTTPSAgent.global, or pass agent: false to use a fresh HTTPSAgent. |
onresponse? | () => void | — | Added as a one-time 'response' listener. |
Functions
createServer
createServer(opts?: HTTPSServerOptions, onrequest?: (req: HTTPIncomingMessage, res: HTTPServerResponse) => void): HTTPSServerCreates an HTTPSServer. If onrequest is given, it's added as a 'request' listener.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
opts? | HTTPSServerOptions | — | Server options: TLS socket options (e.g. cert, key) plus bare-http1 server connection options. |
onrequest? | (req: HTTPIncomingMessage, res: HTTPServerResponse) => void | — | Added as a 'request' listener. |
request
request(url: URL | string, opts?: HTTPSClientRequestOptions, onresponse?: (res: HTTPIncomingMessage) => void): HTTPSClientRequestCreates an HTTPSClientRequest to url (a URL or a URL string), using TLS. If onresponse is given, it's added as a one-time 'response' listener. Does not send the request until it's ended.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
url | URL | string | — | The URL to request, as a URL object or a URL string. |
opts? | HTTPSClientRequestOptions | — | bare-http1 client request options; agent defaults to globalAgent, or pass agent: false to use a fresh HTTPSAgent. |
onresponse? | (res: HTTPIncomingMessage) => void | — | Added as a one-time 'response' listener. |
Constants and variables
globalAgent: HTTPSAgent
Source
The default HTTPSAgent used by request() when no agent option is given.
Types
HTTPSSocketEvents
interface HTTPSSocketEvents extends TLSSocketEvents, TCPSocketEvents {}The events an HTTPSSocket emits: those of both TLSSocket and TCPSocket.
HTTPSSocketOptions
interface HTTPSSocketOptions
extends TLSSocketOptions, TCPSocketOptions, TCPSocketConnectOptions {}Options for HTTPSSocket: those of TLSSocket combined with TCPSocket's connection options.
Classes
HTTPSSocket
Source
class HTTPSSocket {
}See also
- Bare modules — the full
bare-*catalog. - Bare runtime API — the runtime these modules extend.