NodeJS 我必须做什么改变,使https的工作?

6psbrbz9  于 2023-01-04  发布在  Node.js
关注(0)|答案(1)|浏览(138)
const cors = require("cors");
const exp = require("express");
const bp = require("body-parser");
var passport = require("passport");
const https = require('https');
const { connect } = require("mongoose");
var fs = require('fs');
const { success, error } = require("consola");
require('./middlewares/passport');

const { DB, PORT2 } = require("./config");
const options = {
  'key' : fs.readFileSync('./https/key.pem').toString(),
  'cert' : fs.readFileSync('./https/cert.pem').toString()
}
// Initialisation
const app = exp();

// Middlewares
app.use(cors());
app.use(bp.json());
app.use(passport.initialize());

require("./middlewares/passport")(passport);

app.use("/api/users", require("./routes/users"));
app.use("/api/forum", require("./routes/forumRoute"));
app.use("/api/comments", require("./routes/commentRoute"));

https.createServer(options, app)
  app.listen(PORT2, () =>
    success({ message: `Server : ${PORT2}`, badge: true })
  );
  
const startApp = async () => {
  try {
    await connect(DB, {
      useFindAndModify: true,
      useUnifiedTopology: true,
      useNewUrlParser: true,
    });
    success({
      message: `Erfolgreich verbunden mit der Datenbank \n${DB}`,
      badge: true
    });

  } catch (err) {
    error({
      message: ` \n${err}`,
      badge: true
    });
    startApp();
  }
};

startApp();

我无法使用端口3000(即PORT2)访问https://localhost。我已将此信息保存在.env文件中。我使用openssl创建了cert.pem和key.pem
那么我应该做些什么来启动https认证下的服务器呢?我使用以下指令来创建这一切:https://nodejs.org/en/knowledge/HTTP/servers/how-to-create-a-HTTPS-server/

rsl1atfo

rsl1atfo1#

您是否尝试过不使用端口2
如果你试着不这样做,那是不好的做法
使用端口1的欺骗性总是较小

相关问题