不是为了生产。 这些是仅用于本地开发的自签名证书。对于生产,请使用受信任的CA,如 让我们加密。
亮点✨
- 自动生成证书 -不需要OpenSSL
- 智能重用 -验证过期、权限和密钥/证书配对
- 为localhost预配置 - - - - - -
127.0.0.1,::1,localhost无 - 框架就绪 - Express, NestJS, Hono, fasttify
- 安全文件权限 执行(Unix:
600(私钥)
安装📦
需要Node.js >= 22.12。建议作为dev依赖项。
npm install -D generate-certs
# or
pnpm add -D generate-certs
快速启动🚀
import path from "node:path";
import https from "node:https";
import express from "express";
import { generateCerts } from "generate-certs";
const certs = generateCerts({ certsPath: path.resolve(import.meta.dirname, "../certs") });
const app = express();
https.createServer(certs, app).listen(3443);
API参考📖
generateCerts(options): { key: string; cert: string }
从指定的目录生成或检索自签名证书。如果有效证书已经存在,则会重新使用它们。返回 { key: string; cert: string } 作为pem格式的字符串,准备好 https.createServer()。
import path from "node:path";
import { generateCerts } from "generate-certs";
// Auto-generates key.pem and cert.pem (or reuses valid existing ones)
const certs = generateCerts({ certsPath: path.resolve(import.meta.dirname, "../certs") });
console.log(certs.key); // "-----BEGIN RSA PRIVATE KEY-----\n..."
console.log(certs.cert); // "-----BEGIN CERTIFICATE-----\n..."
// Print what happened (reuse, permission fix, backup, generation)
const logged = generateCerts({
certsPath: path.resolve(import.meta.dirname, "../certs"),
logger: console.log,
});
选项: certsPath (必选,证书目录的绝对路径), logger (可选 (message: string) => void;省略时为静音)。
抛出 如果路径无效、不可访问或证书生成失败。
证书详情📜
| 财产 | 价值 |
|---|---|
| 关键算法 | RSA 2048位 |
| 签名 | sha - 256 |
| 有效性 | 1年后的一代 |
| 通用名 | localhost |
| 主题名称 | localhost, 127.0.0.1, ::1 |
| 基本约束 | CA:FALSE (关键) |
| 密钥使用 | 数字签名、密钥加密(关键) |
| 扩展密钥用法 | TLS Web服务器身份验证 |
| 私钥权限 | 600 (Unix) |
智能重用♻️
现有的 key.pem 和 cert.pem 当所有这些条件都满足时,文件才会被重用:
- 文件存在 -两个文件都存在
- 到期 —证书未过期或5分钟内即将过期
- 在此之前 -证书已经有效
- 无 他们包括
localhost,127.0.0.1,::1 - 关键尺寸 -RSA密钥至少为2048位
- 键/ cert配对 -私钥与证书匹配
- Self-signature -自行颁发的证书根据自己的密钥进行验证
来自您自己的CA的证书(例如,mkcert)通过这些检查并保存下来。未选中“公共名称”。
如果 key.pem 可由组或其他人读取,则其模式更改为 600 (Unix)。这些文件被保存了下来。
如果任何检查失败,则将现有文件复制到 key.pem.bak 和 cert.pem.bak (模式 600),则生成一个新的对。较旧的备份永远不会被覆盖; 后来的备份被命名为 key.pem.1.bak, key.pem.2.bak等等。如果备份失败,则不会重新生成任何内容。
框架示例🧩
NestJS
import path from "node:path";
import { NestFactory } from "@nestjs/core";
import { generateCerts } from "generate-certs";
import { AppModule } from "./app.module";
const certs = generateCerts({ certsPath: path.resolve(import.meta.dirname, "../certs") });
async function bootstrap() {
const app = await NestFactory.create(AppModule, { httpsOptions: certs });
await app.listen(3443);
}
bootstrap();
HonoJS
import { createSecureServer } from "node:http2";
import path from "node:path";
import { serve } from "@hono/node-server";
import { generateCerts } from "generate-certs";
import { Hono } from "hono";
const certs = generateCerts({ certsPath: path.resolve(import.meta.dirname, "../certs") });
const app = new Hono();
serve({
fetch: app.fetch,
port: 3443,
createServer: createSecureServer,
serverOptions: certs,
});
Fastify
import path from "node:path";
import Fastify from "fastify";
import { generateCerts } from "generate-certs";
const certs = generateCerts({ certsPath: path.resolve(import.meta.dirname, "../certs") });
async function bootstrap() {
const app = Fastify({ https: certs });
await app.listen({ port: 3443, host: "0.0.0.0" });
}
bootstrap();
最佳实践✅
- 添加
certs/来.gitignore-永远不要将生成的证书提交到版本控制 - 作为dev依赖项安装 (
-D) -在生产环境中不需要这个包 - 预期会出现浏览器警告 -自签名证书将显示 “不安全”; 单击 “高级” → “进入本地主机”
- 默认情况下,日志处于关闭状态 ——通过
logger: console.log看看发生了什么
窗口: Windows系统跳过文件权限检查。
并发性: 锁定文件 (
.generate-certs.lock) 序列化并发调用。只有当它的所有者进程消失或超过60秒时,锁才会被接管。
v4中的重大变化⚠️
- 需要Node.js >= 22.12。
activateLogs被替换为logger。 默认情况下,日志记录是关闭的。通过logger: console.log以恢复v3输出。- Common Name不再被选中。 具有其他cn或无的有效证书将被保留而不是替换。
- 保存ca颁发的证书。 v3需要自签名并替换mkcert样式的证书。
- 一个不安全的
key.pem模式是固定的,没有更换。 键保留其内容并获得模式600。如果无法更改模式,generateCerts抛出。 - 在重新生成之前备份了无效文件 作为
*.bak。 - 对现有证书进行验证
node:crypto(X509Certificate),因此可以接受EC和Ed25519密钥对。 - 生成的证书携带
basicConstraints,keyUsage,extKeyUsage。 node-forge是^1.4.2,它修复了2026年10月建议 (包括PEM重做和签名伪造)。
类型导出🏷️
import type { GenerateCertsOptions } from "generate-certs";
致谢🙏
建立在 node-forge 用于生成RSA密钥和创建X.509证书。
贡献🤝
- 打开一个 问题 或者特性请求
- 提交PR以改进包装
- 如果你觉得它有用,就用星号标记它