In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to start the https server in Node. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
First of all, you need to generate a https certificate, you can go to a paid website to buy or find some free websites, which may end with key or crt or pem. Different formats can be converted through OpenSSL, such as:
Openssl x509-in mycert.crt-out mycert.pem-outform PEM
Node native version:
Const https = require ('https') const path = require (' path') const fs = require ('fs') / / Import the generated certificate file const privateKey = fs.readFileSync (path.join (_ _ dirname,'. / certificate/private.key'), 'utf8') const certificate = fs.readFileSync (path.join (_ _ dirname,'. / certificate/certificate.crt'), 'utf8') const credentials = {key: privateKey, cert: certificate } / / create https server instance const httpsServer = https.createServer (credentials, async (req, res) = > {res.writeHead (200) res.end ('Hello wordings')}) / / set the access port number of https const SSLPORT = 443 const SSLPORT / start the server Listen on the corresponding port httpsServer.listen (SSLPORT, () = > {console.log (`HTTPS Server is running on: https://localhost:${SSLPORT}`)}))
Express version
Const express = require ('express') const path = require (' path') const fs = require ('fs') const https = require (' https') / / Import the generated certificate file const privateKey = fs.readFileSync (path.join (_ dirname,'. / certificate/private.key'), 'utf8') const certificate = fs.readFileSync (path.join (_ dirname,'. / certificate/certificate.crt'), 'utf8') const credentials = {key: privateKey, cert: certificate } / / create express instance const app = express () / / process request app.get ('/', async (req, res) = > {res.status (200). Send ('Hello Worldwide')}) / / create https server instance const httpsServer = https.createServer (credentials, app) / / set the access port number of https const SSLPORT = 443 apperance / start the server Listen on the corresponding port httpsServer.listen (SSLPORT, () = > {console.log (`HTTPS Server is running on: https://localhost:${SSLPORT}`)}))
Koa version
Const koa = require ('koa') const path = require (' path') const fs = require ('fs') const https = require (' https') / / Import the generated certificate file const privateKey = fs.readFileSync (path.join (_ dirname,'. / certificate/private.key'), 'utf8') const certificate = fs.readFileSync (path.join (_ dirname,'. / certificate/certificate.crt'), 'utf8') const credentials = {key: privateKey, cert: certificate } / / create a koa instance const app = koa () / / process the request app.use (async ctx = > {ctx.body = 'Hello wordings'}) / / create a https server instance const httpsServer = https.createServer (credentials, app.callback ()) / / set the access port number of https const SSLPORT = 443MB / start the server Listen to the corresponding port httpsServer.listen (SSLPORT,) = > {console.log (`HTTPS Server is running on: https://localhost:${SSLPORT}`)})) so much about how to start the https server in Node. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.