Https

IoT.js provides HTTPS to support HTTPS clients enabling users to send HTTPS requests easily.

https.createServer([options][, requestListener])

  • options {Object} Accepts the same options as tls.createServer and http.createServer methods.
  • requestListener {Function}
    • request {http.IncomingMessage}
    • response {http.ServerResponse}
  • Returns: {https.Server}

To create a server the certificates should be specified via the options object.

The requestListener is a function which is automatically added to the 'request' event.

Example

  1. var options = {
  2. key: fs.readFileSync('server.key'),
  3. cert: fs.readFileSync('server.cert')
  4. };
  5. var server = https.createServer(options, function(request, response) {
  6. ...
  7. });

https.request(options[, callback])

  • options {Object}
    • host {string} A domain name or IP address of the server to issue the request to. Default: ‘localhost’.
    • hostname {string} Alias for host.
    • port {number} Port of remote server. Default: 443.
    • method {string} A string specifying the HTTPS request method. Default: ‘GET’.
    • path {string} Request path. Default: ‘/‘. Should include query string if any. E.G. ‘/index.html?page=12’. An exception is thrown when the request path contains illegal characters. Currently, only spaces are rejected but that may change in the future.
    • headers {Object} An object containing request headers.
    • auth {string} Optional Basic Authentication in the form username:password. Used to compute HTTPS Basic Authentication header.
    • ca {string} Optional file path to CA certificate. Allows to override system trusted CA certificates.
    • cert {string} Optional file path to client authentication certificate in PEM format.
    • key {string} Optional file path to private keys for client cert in PEM format.
    • rejectUnauthorized {boolean} Optional Specify whether to verify the Server’s certificate against CA certificates. WARNING - Making this false may be a security risk. Default: true
  • callback {Function}
    • response {http.IncomingMessage}
  • Returns: {http.ClientRequest}

Example:

  1. var https = require('https');
  2. var request = https.request({
  3. method: 'POST',
  4. port: 443,
  5. headers: {'Content-Length': 3}
  6. });
  7. ...
  8. request.end();

Note that in the example req.end() was called. With https.request() one must always call req.end() to signify that you’re done with the request - even if there is no data being written to the request body.

https.get(options[, callback])

  • options {Object}
    • host {string} A domain name or IP address of the server to issue the request to. Default: ‘localhost’.
    • hostname {string} Alias for host.
    • port {number} Port of remote server. Default: 443.
    • method {string} A string specifying the HTTPS request method. Default: ‘GET’.
    • path {string} Request path. Default: ‘/‘. Should include query string if any. E.G. ‘/index.html?page=12’. An exception is thrown when the request path contains illegal characters. Currently, only spaces are rejected but that may change in the future.
    • headers {Object} An object containing request headers.
    • auth {string} Optional Basic Authentication in the form username:password. Used to compute HTTPS Basic Authentication header.
    • ca {string} Optional file path to CA certificate. Allows to override system trusted CA certificates.
    • cert {string} Optional file path to client authentication certificate in PEM format.
    • key {string} Optional file path to private keys for client cert in PEM format.
    • rejectUnauthorized {boolean} Optional Specify whether to verify the Server’s certificate against CA certificates. WARNING - Making this false may be a security risk. Default: true
  • callback {Function}
    • response {http.IncomingMessage}
  • Returns: {http.ClientRequest}

Same as https.request except that https.get automatically call req.end() at the end.

Example:

  1. var https = require('https');
  2. https.get({
  3. port: 443,
  4. }, function(res) {
  5. ...
  6. });