注:これらのコマンドはmacOSで実行しました。 Linuxも同じように機能するはずです。 Windowsについては保証しません。
プロジェクトのルートフォルダーで、次のコマンドを実行します。
openssl req -x509 -newkey rsa:2048 -keyout keytmp.pem -out cert.pem -days 365
今すぐ実行:
openssl rsa -in keytmp.pem -out key.pem
これでファイルができましたcert.pem
そしてkey.pem
フォルダ内。
Express / Node.jsでは、次のコードを使用して証明書とキーをロードできます。
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…’)
})
使用している場合create-react-app
、 変更start
のスクリプトpackage.json
ファイル先:
"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?