给定SSL密钥和证书,如何创建HTTPS服务?
当前回答
如果您只需要在本地进行本地开发,那么我已经为这个任务创建了实用程序https://github.com/pie6k/easy-https
import { createHttpsDevServer } from 'easy-https';
async function start() {
const server = await createHttpsDevServer(
async (req, res) => {
res.statusCode = 200;
res.write('ok');
res.end();
},
{
domain: 'my-app.dev',
port: 3000,
subdomains: ['test'], // will add support for test.my-app.dev
openBrowser: true,
},
);
}
start();
It:
将自动添加正确的域条目到/etc/hosts 只在第一次运行/域更改时才会问您admin密码吗 将为给定的域准备https证书 是否信任本地机器上的这些证书 将打开浏览器开始指向您的本地服务器https url
其他回答
Download rar file for openssl set up from here: https://indy.fulgan.com/SSL/openssl-0.9.8r-i386-win32-rev2.zip Just copy your folder in c drive. Create openssl.cnf file and download their content from : http://web.mit.edu/crypto/openssl.cnf openssl.cnf can be put any where but path shoud be correct when we give in command prompt. Open command propmt and set openssl.cnf path C:\set OPENSSL_CONF=d:/openssl.cnf 5.Run this in cmd : C:\openssl-0.9.8r-i386-win32-rev2>openssl.exe Then Run OpenSSL> genrsa -des3 -out server.enc.key 1024 Then it will ask for pass phrases : enter 4 to 11 character as your password for certificate Then run this Openssl>req -new -key server.enc.key -out server.csr Then it will ask for some details like country code state name etc. fill it freely. 10 . Then Run Openssl > rsa -in server.enc.key -out server.key Run this OpenSSL> x509 -req -days 365 -in server.csr -signkey server.key -out server.crt then use previous code that are on stack overflow Thanks
在谷歌搜索“节点https”时发现了这个问题,但接受的答案中的示例非常旧——取自当前(v0.10)版本的节点文档,它应该是这样的:
var https = require('https');
var fs = require('fs');
var options = {
key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
};
https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);
Express API文档非常清楚地说明了这一点。
此外,这个答案给出了创建自签名证书的步骤。
我从Node.js HTTPS文档中添加了一些注释和一个片段:
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);
如果您只需要在本地进行本地开发,那么我已经为这个任务创建了实用程序https://github.com/pie6k/easy-https
import { createHttpsDevServer } from 'easy-https';
async function start() {
const server = await createHttpsDevServer(
async (req, res) => {
res.statusCode = 200;
res.write('ok');
res.end();
},
{
domain: 'my-app.dev',
port: 3000,
subdomains: ['test'], // will add support for test.my-app.dev
openBrowser: true,
},
);
}
start();
It:
将自动添加正确的域条目到/etc/hosts 只在第一次运行/域更改时才会问您admin密码吗 将为给定的域准备https证书 是否信任本地机器上的这些证书 将打开浏览器开始指向您的本地服务器https url
上面的答案很好,但是对于Express和node,这将工作得很好。
由于express为您创建了应用程序,我将跳过这里。
var express = require('express')
, fs = require('fs')
, routes = require('./routes');
var privateKey = fs.readFileSync('cert/key.pem').toString();
var certificate = fs.readFileSync('cert/certificate.pem').toString();
// To enable HTTPS
var app = module.exports = express.createServer({key: privateKey, cert: certificate});
推荐文章
- Angular ng-repeat反过来
- 如何获得请求路径与表达请求对象
- 使用Handlebars 'each'循环访问父对象的属性
- 盎格鲁- ngcloak / ngg展示blink元素
- 禁用表单自动提交按钮单击
- 节点和错误:EMFILE,打开的文件太多
- JavaScript函数中的默认参数值
- 使用RegExp.exec从字符串中提取所有匹配项
- 测试一个值是奇数还是偶数
- 空数组似乎同时等于true和false
- 它是可能的动画scrollTop与jQuery?
- 如何清除下拉框中的所有选项?
- 基于原型的继承与基于类的继承
- 我如何使一个HTML文本框显示空时提示?
- 如何隐藏谷歌隐形reCAPTCHA徽章