In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to use nodejs to build https server". In daily operation, I believe many people have doubts about how to use nodejs to build https server. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use nodejs to build https server". Next, please follow the editor to study!
First of all, we use the https certificate generated in the previous article and nodejs to build a https service, the code is as follows: app.js
Const express = require ("express") const app = express (); const https = require ("https"); const fs = require ("fs"); const options = {key:fs.readFileSync ('. / server.key'), cert:fs.readFileSync (". / server.crt")}
App.get ("/", (req,res) = > {res.send ("hello world");})
Const server = https.createServer (options,app)
Server.listen (3000, () = > {console.log ("running")})
After reading the above code, we use the https module of nodejs to open the https service. There is only one difference between the use of the https module and the use of the http module, that is, https requires a certificate to open the service.
For the article on building services with http module, see here. When https starts a server, you need to pass an options parameter in the form of an object, which has two important attributes. The key attribute represents the private key of the server, and cert represents the certificate issued to the server by the CA organization. Other ways to make it exactly the same as the http module, here we use the express framework to simplify the server building.
Execute node app.js and visit https://localhost:3000/,. The following interface appears in Google browser:
This interface appears because our root certificate is not authoritative, and the public key corresponding to the root certificate is not embedded in major operating systems and browsers, so the browser thinks that our website uses a false certificate and is not safe. Just ignore it here. In real life, our website will not use a self-signed certificate. Here is just a test. Click the Advanced button and the following interface appears:
Click on the solution and go on, then we can visit our website, as shown in the picture.
At this time, it is found that there is a danger sign in the address bar, so just ignore it.
After the https service is built, we build a static file server for https based on this. The code is modified as follows:
Const express = require ("express") const app = express (); const https = require ("https"); const fs = require ("fs"); const options = {key:fs.readFileSync ('. / server.key'), cert:fs.readFileSync (". / server.crt")}
App.get ("/", (req,res) = > {res.send ("hello world");}) app.use (express.static (". / public"))
Const server = https.createServer (options,app); server.listen (3000, () = > {console.log ("running")})
Read the source code carefully, we use the middleware express.static that comes with express. Through this middleware, we publish the static resources of the public file to the server, so we build a static server of https.
To sum up: in this article, we combine the https certificate obtained in the previous article, the https module of nodejs, and the express framework to build a https server, and configure it as a static resource server by using express's middleware express.static.
At this point, the study on "how to use nodejs to build https server" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.