Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build HTTPS server with Node.js

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to build a HTTPS server in Node.js". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

It is very simple to open a HTTP service in Node.js. If you want to use Node.js to open a HTTPS service, you need two steps: one is to generate a signature certificate, and the other is to use the system module HTTPS provided by Node.js.

Self-signed certificate

The certificates usually used for public networks in enterprises are usually issued by the global authoritative CA institutions and trusted by major browser manufacturers. We can self-sign the certificate for simplicity when developing the test, but there will be a security question when this is opened in the browser.

Generate a self-signed certificate using the openssl tool.

It is simply divided into the following steps:

1. Use the genrsa command of openssl to generate a server private key file

# genrsa generation key #-out specifies the output file openssl genrsa-out server.key 2048

two。 Generate a certificate request file

#-new to generate a new certificate request #-key specifies that the entered key openssl req-new-key server.key-out server.csr # will have the following interactive prompt. Common Name can customize the domain name and modify the domain name mapping of the hosts file. Country Name (2 letter code) []: CN State or Province Name (full name) []: ShangHai Locality Name (eg, city) []: ShangHai Organization Name (eg, company) []: Node.js Organizational Unit Name (eg, section) []: Common Name (eg, fully qualified host name) []: test.https.com Email Address []:

3. Generate a certificate based on the certificate request file in step 2 and the server private key file in step 1

# x509 generate a self-signed root certificate based on an existing certificate request #-days sets the valid days of the certificate #-in specifies to enter the certificate request file openssl x509-req-days 365-in server.csr-signkey server.key-out server.crt

After success, the following three files are generated:

Key is the private key file on the server.

Csr is a certificate request signature file that is submitted to the certificate authority CA.

Crt is a certificate signed by the certificate authority CA.

Open a service using the HTTPS module in Node.js

Compared with the traditional HTTP method, there is an extra options parameter to ensure that the self-signed certificate generated above and the file below are the same level directory.

/ / app.js const https = require ('https'); const fs = require (' fs'); const PORT = 8443; const options = {key: fs.readFileSync ('. / server.key'), cert: fs.readFileSync ('. / server.crt')}; https.createServer (options, (req, res) = > {res.writeHead (200); res.end ('Hello Worldlings');}) .customers (PORT, () = > console.log (`App listening on port ${PORT}! `))

After testing, using a self-signed certificate is inaccessible in Chrome version 85.0.4183.121. the following is a screenshot of the access in the 360browser.

The following is accessed in Safari, although prompted but still accessible.

Use in conjunction with the Express framework

After the introduction of the above two steps, it is also easy to use it in frameworks such as Express now, taking Express as an example as follows:

Const express = require ('express'); const https = require (' https'); const fs = require ('fs'); const app = express (); const PORT = 8443; const options = {key: fs.readFileSync ('. / server.key'), cert: fs.readFileSync ('. / server.crt')}; https.createServer (options, app) .customers (PORT, () = > console.log (`App listening on port ${PORT}! `)) App.get ('/', (req, res) = > res.send ('Hello Worldwide')); that's all for "how to build a HTTPS server with Node.js". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report