英文:
HTTPS request hangs forever (https express server w/ openssl self signed certificate)
问题
问题:我正在尝试使用OpenSSL创建一个使用自签名证书的HTTPS服务器。我能够运行服务器,但当我向"https://localhost:3001"发出请求时,它会永远挂在"发送请求..."上。
我尝试在浏览器(Brave和Chrome)以及Postman中发出请求,都出现了永远加载的相同问题。我不确定我做错了什么?
英文:
Issue: I'm trying to create an HTTPS server using a self signed certificate on openssl. I am able to run the server but when I make a request to "https://localhost:3001", it hangs forever on "Sending request...".
I've tried making the request in the browser (Brave and Chrome) and in Postman, both with the same issue of loading forever. I'm not sure what I am doing wrong?
答案1
得分: 0
I am not quite sure about, if it will work for you. In my case I was able to fix such issue by including a return
statement in the endpoint. Two changes you may try in this are listed below
// Endpoints
app.get('/',(req,res)=> res.send("Hello, World!")); //automatic return
or
// Endpoints
app.get('/',(req,res)=>{
return res.send("Hello, World!");
}
I would also suggest you to add some console.log
statements inside the end points, it is good practice to follow and help you debug the request very easily, at least we can understand where the request is actually hanging stuck.
英文:
I am not quite sure about, if it will work for you. In my case I was able to fix such issue by including a return
statement in the endpoint. Two changes you may try in this are listed below
// Endpoints
app.get('/',(req,res)=> res.send("Hello, World!"); //automatic return
or
// Endpoints
app.get('/',(req,res)=>{
return res.send("Hello, World!");
}
I would also suggest you to add some console.log
statements inside the end points, it is good practice to follow and help you debug the request very easily, at least we can understand where the request is actually hanging stuck.
答案2
得分: 0
你的代码中没有使用变量 app
。因此,你的API没有任何端点等。
要修复这个问题,需要将第二个参数添加到 createServer
中,传入你的 Express.js 应用程序,如下所示:
https.createServer({
key: privateKey,
cert: certificate
}, app).listen(port);
请注意,这里的第二个参数是 app
。
然后你的服务器将有一个根端点(/)并且你应该会收到结果。
注意:你不需要使用 return res.send("Hello World!");
。
只需要使用 res.send("Hello World!");
就足够了。
英文:
You are not using the variable app
anywhere in your code.
So your API does not have any endpoints, etc.
To fix it add second parameter to createServer with your express.js app like so:
https.createServer({
key: privateKey,
cert: certificate
}, app).listen(port);
Notice the second parameter here is app
.
Then your server will have the / endpoint and you should get results.
Note: You don't need to do return res.send("Hello World!");
.
Just doing res.send("Hello World!");
is enough.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论