Secure outbound requests
Call an external HTTPS endpoint and process its streamed response.
Node.js · Module
node:https provides Node.js client and server APIs for HTTP over TLS/SSL. It supports secure requests, HTTPS servers, connection agents, and TLS-specific configuration.
The node:https module supplies HTTPS client and server APIs. Use https.get() for a GET request or https.request() for fuller control, including TLS settings; requests return writable HTTP client request streams, while servers listen for encrypted connections.
import * as https from 'node:https';
https.get(url[, options][, callback]);
https.request(options[, callback]);
https.createServer([options][, requestListener]);| Name | Type | Description |
|---|---|---|
url/options | string | URL | Object | Request target or request configuration; https.get() accepts a URL, string, or options object, while https.request() accepts options. |
callback | Function | Function invoked with the incoming response or request event, depending on the API. |
https.get() returns an http.ClientRequest; https.request() returns an http.ClientRequest; https.createServer() returns an HTTPS server.
https.get() when a secure GET request is sufficient.https.request() when you need a configurable secure client request.node:crypto support cannot import or require this module successfully.import https from 'node:https';
https.get('https://example.com', (response) => {
let body = '';
response.setEncoding('utf8');
response.on('data', (chunk) => { body += chunk; });
response.on('end', () => console.log(response.statusCode, body.length));
}).on('error', console.error);Performs a secure GET request, collects the streamed response body, and reports the status code and size.
Call an external HTTPS endpoint and process its streamed response.
Expose an application endpoint with encrypted transport.
Reuse or customize secure client connections.
https.get() differ from https.request()?https.get() is the HTTPS counterpart of http.get(). It accepts a URL or options, sets the request method to GET, and returns an HTTP client request whose response arrives through events.
Follow-up: How would you configure mutual TLS? Supply appropriate TLS options such as key, cert, and ca through the request options.
https.request() return?https.request() makes a request to a secure web server and returns an http.ClientRequest. The returned request is a writable stream, so request data can be written before ending it.
Follow-up: What if the request must reuse or customize connections? Configure an HTTPS agent; its createConnection() method can be overridden for TLS connection customization.
https.createServer()?https.createServer() creates a server for encrypted connections. Its listening behavior follows net.Server.listen(), while TLS credentials and other server options configure the secure transport.
Follow-up: What should be checked when a client rejects the server certificate? Verify the certificate chain, hostname, trust configuration, and relevant TLS options rather than disabling validation by default.
https.Agenthttps.createServer()https.get()https.globalAgenthttps.request()http.ClientRequesttls.connect()