差别
这里会显示出您选择的修订版和当前版本之间的差别。
it:node:localhost_ssl [2023-04-23 16:20] – 创建 goldentianya | it:node:localhost_ssl [2023-04-23 16:25] (当前版本) – goldentianya | ||
---|---|---|---|
行 6: | 行 6: | ||
Open a terminal or command prompt and navigate to the directory where you want to store the certificate files. | Open a terminal or command prompt and navigate to the directory where you want to store the certificate files. | ||
- | Generate a private key: | + | Generate a private key:<color # |
- | csharp | ||
- | Copy code | ||
- | openssl genrsa -out localhost.key 2048 | ||
Generate a certificate signing request (CSR): | Generate a certificate signing request (CSR): | ||
- | vbnet | + | |
- | Copy code | + | <color # |
- | openssl req -new -key localhost.key -out localhost.csr | + | |
When prompted for information, | When prompted for information, | ||
Generate a self-signed certificate: | Generate a self-signed certificate: | ||
- | csharp | + | |
- | Copy code | + | <color # |
- | openssl x509 -req -days 365 -in localhost.csr -signkey localhost.key -out localhost.crt | + | |
This will generate a self-signed certificate that is valid for one year. | This will generate a self-signed certificate that is valid for one year. | ||
Start your Node.js server and configure it to use the certificate and private key: | Start your Node.js server and configure it to use the certificate and private key: | ||
- | javascript | + | |
- | Copy code | + | <code | download> |
const https = require(' | const https = require(' | ||
const fs = require(' | const fs = require(' | ||
行 41: | 行 38: | ||
server.listen(8443, | server.listen(8443, | ||
console.log(' | console.log(' | ||
- | }); | + | }) |
+ | </ | ||
You can then access your Node.js server using the URL https:// | You can then access your Node.js server using the URL https:// | ||
Keep in mind that self-signed certificates are not trusted by default, so if you want to use SSL/TLS in a production environment, | Keep in mind that self-signed certificates are not trusted by default, so if you want to use SSL/TLS in a production environment, | ||
- | |||
- | |||
- | |||
- | |||
- | Regenerate response | ||