gpt4 book ai didi

javascript - 如何在 Node.js 中创建 HTTPS 服务器?

转载 作者:IT老高 更新时间:2023-10-28 11:04:14 24 4
gpt4 key购买 nike

给定一个 SSL key 和证书,如何创建 HTTPS 服务?

最佳答案

Express API doc很清楚地说明了这一点。

另外 this answer给出了创建自签名证书的步骤。

我添加了一些评论和来自 Node.js HTTPS documentation 的片段:

var express = require('express');
var https = require('https');
var http = require('http');
var fs = require('fs');

// This line is from the Node.js HTTPS documentation.
var options = {
key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
cert: fs.readFileSync('test/fixtures/keys/agent2-cert.cert')
};

// Create a service (the app object is just a callback).
var app = express();

// Create an HTTP service.
http.createServer(app).listen(80);
// Create an HTTPS service identical to the HTTP service.
https.createServer(options, app).listen(443);

关于javascript - 如何在 Node.js 中创建 HTTPS 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5998694/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com