Creating an HTTPS server is almost exactly like creating an HTTP server, discussed earlier in this chapter. The only difference is that there are additional options that you must pass into https.createServer()
. The options, listed in Table 7.9, allow you to specify the security for the server. The most important options are key
and cert
.
The key
option specifies the private key used for SSL. The cert
value specifies the x509 public key to use. The following is an example of creating an HTTPS server in Node.js:
var options = {
key: fs.readFileSync('test/keys/server.pem'),
cert: fs.readFileSync('test/keys/server.crt')
};
https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("Hello Secure World
");
}).listen(8080);
Once the HTTPS server has been created, the request/response handling works the same way as in HTTP servers, described earlier in this chapter.