给定SSL密钥和证书,如何创建HTTPS服务?


当前回答

你也可以在Fastify框架中使用存档:

const { readFileSync } = require('fs')
const Fastify = require('fastify')

const fastify = Fastify({
  https: {
    key: readFileSync('./test/asset/server.key'),
    cert: readFileSync('./test/asset/server.cert')
  },
  logger: { level: 'debug' }
})

fastify.listen(8080)

(然后执行openssl req -nodes -new -x509 -keyout server命令。密钥输出服务器。如果需要编写测试,请使用证书来创建文件)

其他回答

对于Node 0.3.4及以上版本,一直到当前的LTS(在此编辑时为v16), https://nodejs.org/api/https.html#httpscreateserveroptions-requestlistener有您需要的所有示例代码:

const https = require(`https`);
const fs = require(`fs`);

const options = {
  key: fs.readFileSync(`test/fixtures/keys/agent2-key.pem`),
  cert: fs.readFileSync(`test/fixtures/keys/agent2-cert.pem`)
};

https.createServer(options, (req, res) => {
  res.writeHead(200);
  res.end(`hello world\n`);
}).listen(8000);

请注意,如果想使用certbot工具使用Let's Encrypt的证书,则私钥称为privkey。Pem,证书名为fullchain.pem:

const certDir = `/etc/letsencrypt/live`;
const domain = `YourDomainName`;
const options = {
  key: fs.readFileSync(`${certDir}/${domain}/privkey.pem`),
  cert: fs.readFileSync(`${certDir}/${domain}/fullchain.pem`)
};

上面的答案很好,但是对于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});

在Node.js中HTTPS服务器的最小设置是这样的:

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

var httpsOptions = {
    key: fs.readFileSync('path/to/server-key.pem'),
    cert: fs.readFileSync('path/to/server-crt.pem')
};

var app = function (req, res) {
  res.writeHead(200);
  res.end("hello world\n");
}

https.createServer(httpsOptions, app).listen(4433);

如果你也想支持http请求,你需要做一个小小的修改:

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

var httpsOptions = {
    key: fs.readFileSync('path/to/server-key.pem'),
    cert: fs.readFileSync('path/to/server-crt.pem')
};

var app = function (req, res) {
  res.writeHead(200);
  res.end("hello world\n");
}

http.createServer(app).listen(8888);
https.createServer(httpsOptions, app).listen(4433);

你也可以在Fastify框架中使用存档:

const { readFileSync } = require('fs')
const Fastify = require('fastify')

const fastify = Fastify({
  https: {
    key: readFileSync('./test/asset/server.key'),
    cert: readFileSync('./test/asset/server.cert')
  },
  logger: { level: 'debug' }
})

fastify.listen(8080)

(然后执行openssl req -nodes -new -x509 -keyout server命令。密钥输出服务器。如果需要编写测试,请使用证书来创建文件)

如果您只需要在本地进行本地开发,那么我已经为这个任务创建了实用程序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