想在 Node.js 中将本地主机的 HTTP 转换为 HTTPS……遇到了某种错误。

huangapple go评论72阅读模式
英文:

Want to convert localhost http to https in node js .........Got some sort of error

问题

这是我的代码,我不知道如何在这里使用它。尽管它在简单的情况下是有效的:

https.createServer(options)

但我希望它在整个本地主机项目上运行,即"app"。我尝试通过以下方式运行它:

const server = https.createServer(app[options]);
server.listen(process.env.PORT, (req, res) => {
  console.log("hii")
})

但出现了这个错误:
[这是错误,同时也让我显示代码][1]
[1]: https://i.stack.imgur.com/jNVph.png

const https = require("https");

const fs = require('fs');
const path = require("path");
var cons = require("consolidate");
var express = require("express");
var app = express();
const options = {
  key: fs.readFileSync('certificates/key.pem'),
  cert: fs.readFileSync('certificates/cert.pem')
}

const server = https.createServer(app[options]);
const dotenv = require("dotenv");
dotenv.config();
const signuprouter = require("./Controllers/signUpController");
const loginrouter = require("./Controllers/loginController");
const dashbaordrouter = require("./Controllers/dashboardController");
const hotelrouter = require("./Controllers/hotelsController")
const userDashboard = require("./Controllers/userDashboard")
const cors = require("cors");
const swig = require("swig");
app.use(express.json())

app.engine("html", cons.swig);
app.set("views", path.join(__dirname, "pages/front-end"));
app.set("view engine", "html");
app.use(express.static(path.join(__dirname, "/public")));
app.use("/", signuprouter);
app.use("/", loginrouter);
app.use("/", dashbaordrouter);
app.use("/", hotelrouter);
app.use("/", userDashboard);

server.listen(process.env.PORT, (req, res) => {
  console.log("hii")
})
module.exports = server;
英文:

So here is my code i dont know how to use it with this.Although its working with simpple

https.createserver(options)

but i want it to run on whole localhost project that is "app".i tried to run it by this

const server = https.createServer(app[options]);
server.listen(process.env.PORT, (req, res) => {
console.log("hii")
})

but this error is occuring:
[Here is the error let me show the code as well][1]
[1]: https://i.stack.imgur.com/jNVph.png

const https = require("https");
const fs=require('fs')
const path = require("path");
var cons = require("consolidate");
var express = require("express");
var app = express();
const options ={
key:fs.readFileSync('certificates/key.pem'),
cert:fs.readFileSync('certificates/cert.pem') 
}
const server = https.createServer(app[options]);
const dotenv = require("dotenv");
dotenv.config();
const signuprouter = require("./Controllers/signUpController");
const loginrouter = require("./Controllers/loginController");
const dashbaordrouter = require("./Controllers/dashboardController");
const hotelrouter=require("./Controllers/hotelsController")
const userDashboard=require("./Controllers/userDashboard")
const cors = require("cors");
const swig = require("swig");
app.use(express.json())
app.engine("html", cons.swig);
app.set("views", path.join(__dirname, "pages/front-end"));
app.set("view engine", "html");
app.use(express.static(path.join(__dirname, "/public")));
app.use("/", signuprouter);
app.use("/", loginrouter);
app.use("/", dashbaordrouter);
app.use("/", hotelrouter);
app.use("/", userDashboard);
server.listen(process.env.PORT, (req, res) => {
console.log("hii")
})
module.exports=server;

答案1

得分: 0

createServer接受一个指定凭据的选项对象和一个可能是Express应用程序的中间件函数

const server = https.createServer(options, app);
server.listen(process.env.PORT, (req, res) => {
  console.log("你好")
})
英文:

createServer takes an options object that specifies the credentials and a middleware function that can be an express application.

const server = https.createServer(options, app);
server.listen(process.env.PORT, (req, res) => {
console.log("hii")
})

huangapple
  • 本文由 发表于 2023年1月10日 00:51:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75060498.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定