Open In App

Node.js server.addContext() Method

Last Updated : 06 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The server.addContext() is an inbuilt application programming interface of class Socket within tls module which is used to add the security context for the use of the client.

Syntax:

server.addContext(hostname, context)

Parameters: This method takes the following argument as a parameter:

  • hostname: A server name indicating hostname.
  • context: Any of the properties from the tls.createSecureContext() options arguments.

Return Value: This method does not return any value.

How to generate private keys and public certificates?

Private key: Open Notepad and copy and paste the following key:

-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQC38R9wXcUbhOd44FavgmE5R3K4JeYOHLnI7dUq1B8/Gv7l3SOg
JKef/m9gM1KvUx951mapXGtcWgwB08J3vUE2YOZ4tWJArrVZES0BI/RmFAyhQFP5
HcWl3LSM9LRihP98F33oIkKaCxA5LxOrkgpV4HrUzIKTABDYah7RPex1WQIDAQAB
AoGBAIXR71xxa9gUfc5L7+TqBs+EMmrUb6Vusp8CoGXzQvRHMJCMrMFySV0131Nu
o0YYRDsAh1nJefYLMNcXd1BjqI+qY8IeRsxaY+9CB2KKGVVDO2uLdurdC2ZdlWXT
Vwr3dDoyR0trnXJMmH2ijTeO6bush8HuXxvxJBjvEllM5QYxAkEA3jwny9JP+RFu
0rkqPBe/wi5pXpPl7PUtdNAGrh6S5958wUoR4f9bvwmTBv1nQzExKWu4EIp+7vjJ
fBeRZhnBvQJBANPjjge8418PS9zAFyKlITq6cxmM4gOWeveQZwXVNvav0NH+OKdQ
sZnnDiG26JWmnD/B8Audu97LcxjxcWI8Jc0CQEYA5PhLU229lA9EzI0JXhoozIBC
TlcKFDuLm88VSmlHqDyqvF9YNOpEdc/p2rFLuZS2ndB4D+vu6mjwc5iZ3HECQCxy
GBHRclQ3Ti9w76lpv+2kvI4IekRMZWDWnnWfwta+DGxwCgw2pfpleBZkWqdBepb5
JFQbcxQJ0wvRYXo8qaUCQQCgTvWswBj6OTP7LTvBlU1teAN2Lnrk/N5AYHZIXW6m
nUG9lYvH7DztWDTioXMrruPF7bdXfZOVJD8t0I4OUzvC
-----END RSA PRIVATE KEY-----

Save the file as a private key.pem

Public certificate: Open Notepad and copy and paste the following key:

-----BEGIN CERTIFICATE-----
MIICfzCCAegCCQDxxeXw914Y2DANBgkqhkiG9w0BAQsFADCBgzELMAkGA1UEBhMC
SU4xEzARBgNVBAgMCldlc3RiZW5nYWwxEDAOBgNVBAcMB0tvbGthdGExFDASBgNV
BAoMC1BhbmNvLCBJbmMuMRUwEwYDVQQDDAxSb2hpdCBQcmFzYWQxIDAeBgkqhkiG
9w0BCQEWEXJvZm9mb2ZAZ21haWwuY29tMB4XDTIwMDkwOTA1NTExN1oXDTIwMTAw
OTA1NTExN1owgYMxCzAJBgNVBAYTAklOMRMwEQYDVQQIDApXZXN0YmVuZ2FsMRAw
DgYDVQQHDAdLb2xrYXRhMRQwEgYDVQQKDAtQYW5jbywgSW5jLjEVMBMGA1UEAwwM
Um9oaXQgUHJhc2FkMSAwHgYJKoZIhvcNAQkBFhFyb2ZvZm9mQGdtYWlsLmNvbTCB
nzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAt/EfcF3FG4TneOBWr4JhOUdyuCXm
Dhy5yO3VKtQfPxr+5d0joCSnn/5vYDNSr1MfedZmqVxrXFoMAdPCd71BNmDmeLVi
QK61WREtASP0ZhQMoUBT+R3Fpdy0jPS0YoT/fBd96CJCmgsQOS8Tq5IKVeB61MyC
kwAQ2Goe0T3sdVkCAwEAATANBgkqhkiG9w0BAQsFAAOBgQATe6ixdAjoV7BSHgRX
bXM2+IZLq8kq3s7ck0EZrRVhsivutcaZwDXRCCinB+OlPedbzXwNZGvVX0nwPYHG
BfiXwdiuZeVJ88ni6Fm6RhoPtu2QF1UExfBvSXuMBgR+evp+e3QadNpGx6Ppl1aC
hWF6W2H9+MAlU7yvtmCQQuZmfQ==
-----END CERTIFICATE-----

Save the file as public-cert.pem

Example 1: Filename: index.js

javascript




// Node.js program to demonstrate the
// server.addContext() method
 
const tls = require('tls'),
    fs = require('fs'),
 
    // Port and host address for server
    PORT = 1337,
    HOST = '127.0.0.1';
 
// Private key and public certificate for access
const options = {
    key: fs.readFileSync('private-key.pem'),
    cert: fs.readFileSync('public-cert.pem'),
    rejectUnauthorized: false
};
 
// Creating and initializing server
const server = tls.createServer(options,
    function (socket) {
 
        // Print the data that we received
        socket.on("data", function (data) {
            console.log('\nReceived: %s ',
                data.toString().replace(/(\n)/gm, ""));
        });
 
    });
 
// Adding context into the server
server.addContext('127.0.0.2', options.key);
console.log("context is added");
 
// Start listening on a specific
// port and address
server.listen(PORT, HOST, function () {
    console.log("I'm listening at %s, "
        + "on port %s", HOST, PORT);
});
 
// When an error occurs, show it.
server.on("error", function (error) {
 
    console.error(error);
    // Close the connection after the
    // error occurred.
    // server.destroy();
});
 
server.on("end", () => {
    server.end();
    //server.destroy();
})
 
 
// Creating and initializing client
const client = tls.connect(PORT, HOST,
    options, function () {
 
        // Check if the authorization worked
        if (client.authorized) {
            console.log("Connection authorized"
                + " by a Certificate Authority.");
        } else {
            console.log("Connection not authorized: "
                + client.authorizationError)
        }
 
        // Send a friendly message
        client.write(
            "I am the client sending you a message.");
        client.end();
    });
 
client.on("close", function () {
    console.log("Connection closed");
});
 
// When an error occurs, show it.
client.on("error", function (error) {
 
    console.error(error);
 
    // Close the connection after
    // the error occurred.
    client.destroy();
});


Output:

context is added
I'm listening at 127.0.0.1, on port 1337
Connection not authorized: DEPTH_ZERO_SELF_SIGNED_CERT

Received: I am the client sending you a message.
Connection closed

Example 2: Filename: index.js

javascript




// Node.js program to demonstrate the
// server.addcontext() method
 
const tls = require('tls'),
    fs = require('fs'),
 
    // Port and host address for server
    PORT = 1337,
    HOST = '127.0.0.1';
 
// Private key and public certificate for access
const options = {
    key: fs.readFileSync('private-key.pem'),
    cert: fs.readFileSync('public-cert.pem'),
    rejectUnauthorized: false
};
 
// Creating and initializing server
const server = tls.createServer(options,
    function (socket) {
 
        // Print the data that we received
        socket.on('data', function (data) {
            console.log('\nReceived: %s ',
                data.toString()
                    .replace(/(\n)/gm, ""));
        });
    });
 
// Adding context into the server
// by using addContext() method
server.addContext('127.0.0.2', options.key);
console.log("context is added");
 
// Start listening on a specific
// port and address
server.listen(PORT, HOST, function () {
    console.log("I'm listening at %s, "
        + "on port %s", HOST, PORT);
});
 
// Creating and initializing client
const client = tls.connect(PORT,
    HOST, options, function () {
 
        // Send a friendly message
        client.write("I am the client "
            + "sending you a message.");
        client.end();
    });


Run the index.js file using the following command:

node index.js

Output:

context is added
I'm listening at 127.0.0.1, on port 1337

Received: I am the client sending you a message.

Reference: https://nodejs.org/dist/latest-v12.x/docs/api/tls.html#tls_server_addcontext_hostname_context



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads