打开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");
});
回复 (0)
微信扫码 立即评论