Lưu ý: Tôi đã chạy các lệnh này trên macOS. Linux sẽ hoạt động theo cách tương tự. Tôi không đảm bảo cho Windows.
Trong thư mục gốc của dự án, hãy chạy:
openssl req -x509 -newkey rsa:2048 -keyout keytmp.pem -out cert.pem -days 365
Bây giờ chạy:
openssl rsa -in keytmp.pem -out key.pem
Bây giờ bạn sẽ có các tệpcert.pem
vàkey.pem
trong thư mục.
Với Express / Node.js, bạn có thể tải chứng chỉ và khóa bằng mã này:
const fs = require('fs')
const https = require('https')
const app = express()
app.get(’/’, (req, res) => {
res.send(‘Hello HTTPS!’)
})
https.createServer({
key: fs.readFileSync(‘key.pem’),
cert: fs.readFileSync(‘cert.pem’)
}, app).listen(3000, () => {
console.log(‘Listening…’)
})
Nếu bạn đang sử dụngcreate-react-app
, thay đổistart
kịch bản trongpackage.json
nộp vào:
"start": "export HTTPS=true&&SSL_CRT_FILE=cert.pem&&SSL_KEY_FILE=key.pem react-scripts start",Look at your framework/library documentation on the instructions on how to pass the certificate and key to the app.
More network tutorials:
- Introduction to WebSockets
- How HTTP requests work
- The HTTP Request Headers List
- The HTTP Response Headers List
- HTTP vs HTTPS
- What is an RFC?
- The HTTP protocol
- The HTTPS protocol
- The curl guide to HTTP requests
- Caching in HTTP
- The HTTP Status Codes List
- What is a CDN?
- The HTTP/2 protocol
- What is a port
- DNS, Domain Name System
- The TCP Protocol
- The UDP Protocol
- An introduction to REST APIs
- How to install a local SSL certificate in macOS
- How to generate a local SSL certificate
- How to configure Nginx for HTTPS
- A simple nginx reverse proxy for serving multiple Node.js apps from subfolders
- What is a reverse proxy?