Setting Up HTTPS Servers with Node.js
Providing a secure connection for a Node.js application often involves setting up an HTTPS server. This allows data transmission between the client and server to be encrypted, ensuring its confidentiality and integrity.
Creating an HTTPS Service with Certificates
To establish an HTTPS service, the server needs an SSL certificate and key. Here's how to do it:
<code class="javascript">const express = require('express'); const https = require('https'); const fs = require('fs'); // Load the SSL certificate and key const options = { key: fs.readFileSync('certificate.key'), cert: fs.readFileSync('certificate.crt') }; // Create an Express application const app = express(); // Use the HTTPS module to create an HTTPS server const server = https.createServer(options, app); // Start the server on a secure port (e.g., 443) server.listen(443, () => { console.log('HTTPS server listening on port 443'); });</code>
Additional Notes
The above is the detailed content of How to Set Up an HTTPS Server in Node.js for Secure Data Transmission?. For more information, please follow other related articles on the PHP Chinese website!