OpenSSL创建自签证书,Node.JS启动HTTPS服务器


发布者 ourjs  发布时间 1668497332703
关键字 Node.JS 

打开Linux上命令行终端,输入以下命令:·

openssl req -nodes -new -x509 -keyout server.key -out server.cert

 

运行之后,需要填写一些资料:

Common Name (e.g. server FQDN or your name): localhost
Email Address : *************@****** (enter your email)

最后两条填写域名和邮箱

用此证书可启动HTTPS服务器,如:

 // Requiring in-built https for creating
// https server
const https = require("https");

// Express for handling GET and POST request
const express = require("express");
const app = express();

// Requiring file system to use local files
const fs = require("fs");

// Parsing the form of body to take
// input from forms
const bodyParser = require("body-parser");

// Configuring express to use body-parser
// as middle-ware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// Get request for root of the app
app.get("/", function (req, res) {

// Sending index.html to the browser
res.sendFile(__dirname + "/index.html");
});

// Post request for geetting input from
// the form
app.post("/mssg", function (req, res) {

// Logging the form body
console.log(req.body);

// Redirecting to the root
res.redirect("/");
});

// Creating object of key and certificate
// for SSL
const options = {
key: fs.readFileSync("server.key"),
cert: fs.readFileSync("server.cert"),
};

// Creating https server by passing
// options and app object
https.createServer(options, app)
.listen(3000, function (req, res) {
console.log("Server started at port 3000");
});

 









 热门文章 - 分享最多
  1. 使用node.js\Express\TypeScript\Nodemon和EsLint创建项目,并通过babel导入import绝对路径
  2. Rust 中的闭包及捕获上下文环境变量使用和更改及闭包的引用
  3. Flex布局基础概念及入门实例教程
  4. 纯CSS实现圆形动画进度百分比饼图Percentage Circle with Animation
  5. puppeteer等自动化测试框架如何判断CSS动画结束animation end
  6. Kubernetes/k8s/docker常用命令实例简单介绍教程
  7. 用Node.JS写一个简单版的类似pm2\forever的守护进程并记录错误日志
  8. MongoDB起步入门教程使用Node.JS的Promise方式查找数据
  9. Node.JS连接MySQL数据库执行增删改查提示ER_NOT_SUPPORTED_AUTH_MODE: Client does not support;如何在@mysql/xdevapi执行SQL
  10. NodeJS在Windows上使用OLE/COM/ActiveXObject对象连接ADODB数据库,操作Application.Excel、运行VBA宏并另存为网页

 相关阅读
  1. puppeteer窗口最大化及改变窗口大小
  2. node.js中async/await不用try/catch,使用error first或go语言方法处理异常
  3. JavaScript和node.js内存泄露的原因和避免方法及Chrome调试工具使用教程
  4. node.js性能压力测试入门教程:wrk和loadtest安装使用
  5. Html5网页中用JavaScript调用本地手机摄像头扫描识别微信二维码、条形码:postMessage跨域https传递扫码结果消息
  6. node.js通过Error.prepareStackTrace获取上层调用函数的文件名地址和行数位置
  7. webpack前端项目调试环境安装入门:webpack.config.js禁用UglifyJs只合并JavaScript不压缩混淆代码
  8. node.js创建aria2代理服务器:使用net.socket转发rpc或http request请求,替换websocket
  9. request停止维护:用node.js实现http网页爬虫抓取,模拟ajax\post请求,大文件上传下载
  10. 用node.js在Windows或Linux平台上高性能解压/压缩zip、tar大文件,输出到文件或Stream流

  开源的 OurJS
OurJS开源博客已经迁移到 OnceOA 平台。

  关注我们
扫一扫即可关注我们:
OnceJS

OnceOA