Nodejs builds https locally

王林
Release: 2023-05-23 16:56:37
Original
572 people have browsed it

With the continuous development of Internet technology and applications, security issues have attracted more and more attention. Transmission security has become a necessity for modern network applications. In this context, HTTPS (Hypertext Transfer Protocol over Secure Socket Layer) has become the main method of data transmission on the Internet. It can realize encrypted transmission of data through the SSL/TSL protocol, greatly improving the security of data transmission.

When using HTTPS, you need to use a certificate on the server side. The certificate can be used to prove the identity of the server and ensure the security during information transmission. In this article, we will introduce how to build an HTTPS server with a local certificate in Node.js.

How to generate local certificates:
First, we need some trusted certificates to establish an HTTPS connection. The root certificate of each operating system may be different. How to generate a self-signed certificate? Just use the tool openssl.

We take the Linux system as an example and execute the following command:

openssl genrsa -out private.key 1024
openssl req -new -key private.key -out cert.csr
openssl x509 -req -in cert.csr -signkey private.key -out cert.crt

After the above commands are executed in sequence, three files will be generated: private.key, cert.csr, cert.crt .

Among them, private.key is the private key and cert.crt is the certificate. cert.csr is the certificate signing request and this file can be ignored.

Next, we can use these files to build an HTTPS server.

Node.js uses HTTPS to build a local server:
First, we need to create a Node.js server file, such as app.js. In this file, we need to introduce the https and fs modules:

const https = require('https');
const fs = require('fs');

Next, we need to define some parameters. In this example, we need to define the certificate file and port number:

const options = {
key: fs.readFileSync('private.key'),
cert: fs.readFileSync('cert.crt')
};
const port = 3000;

Among them, key and cert are the certificate files generated above.

Next, we need to create an HTTPS server and listen on the port number:

const server = https.createServer(options, (req, res) => {
res.writeHead (200);
res.end('hello world
');
});

server.listen(port, () => {
console.log (server listening on port ${port});
});

Now we can run the Node.js server file by running the following command in the command line:

$ node app.js

When we enter https://localhost:3000 in the browser, the browser will find that the website is not safe because our certificate is currently a self-signed certificate. The server does not consider this certificate to be credible.

To make the browser trust this certificate, we need to import the certificate into the browser or purchase a formal certificate.

To import the certificate, we can open the https://localhost:3000 website in Chrome, click the Advanced button, then click the "Continue to localhost (unsafe)" link, and then right-click on the website , select View Certificate, and then on the certificate details page, click Copy Public Key. Next, open chrome://settings/certificates in the browser, then click the "Other Actions" > "Import" button, in the pop-up "Certificate Import Wizard", select "Import Certificate from Clipboard", and then press Just follow the instructions.

In this way, we can build an HTTPS server locally. In practical applications, formal certificates should be used to ensure communication security.

The above is the detailed content of Nodejs builds https locally. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!