Home >Web Front-end >JS Tutorial >How to Create an HTTPS Server with Express.js and Node.js without Using SSL?
Creating an HTTPS Server in Node.js
Given an SSL key and certificate, let's explore how to create an HTTPS service using Express.js and Node.js.
Implementation:
Express's API documentation clearly outlines the steps:
Self-Signed Certificate:
In the provided answer, additional steps are included for creating a self-signed certificate.
Example:
Below is a code snippet illustrating the process:
<code class="javascript">var express = require('express'); var https = require('https'); var http = require('http'); var fs = require('fs'); // SSL options with key and certificate var options = { key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'), cert: fs.readFileSync('test/fixtures/keys/agent2-cert.cert') }; // Express app instance var app = express(); // Create HTTP and HTTPS servers http.createServer(app).listen(80); // HTTP service on port 80 https.createServer(options, app).listen(443); // HTTPS service on port 443</code>
The above is the detailed content of How to Create an HTTPS Server with Express.js and Node.js without Using SSL?. For more information, please follow other related articles on the PHP Chinese website!