Home > Article > Web Front-end > How to Establish and Configure HTTPS Servers in Node.js?
Creating HTTPS Servers in Node.js
To establish an HTTPS service with a provided SSL key and certificate, the Express API documentation offers clear guidance.
Creating Self-Signed Certificates
In addition, the provided answer describes the steps involved in generating a self-signed certificate.
Creating HTTPS Servers with Express
Here's a commented code snippet adapted from the Node.js HTTPS documentation:
var express = require('express'); var https = require('https'); var http = require('http'); var fs = require('fs'); // Define SSL options using the provided key and certificate. var options = { key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'), cert: fs.readFileSync('test/fixtures/keys/agent2-cert.cert') }; // Create an Express application (a callback function). var app = express(); // Create an HTTP service. http.createServer(app).listen(80); // Create an HTTPS service identical to the HTTP service. https.createServer(options, app).listen(443);
This code creates both HTTP and HTTPS services using the provided SSL key and certificate, allowing secure communication over HTTPS.
The above is the detailed content of How to Establish and Configure HTTPS Servers in Node.js?. For more information, please follow other related articles on the PHP Chinese website!