英文:
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")
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论