HTTPS请求永远挂起(带有自签名证书的https express服务器)

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

HTTPS request hangs forever (https express server w/ openssl self signed certificate)

问题

问题:我正在尝试使用OpenSSL创建一个使用自签名证书的HTTPS服务器。我能够运行服务器,但当我向"https://localhost:3001"发出请求时,它会永远挂在"发送请求..."上。

我尝试在浏览器(Brave和Chrome)以及Postman中发出请求,都出现了永远加载的相同问题。我不确定我做错了什么?

以下是我的服务器代码:
HTTPS请求永远挂起(带有自签名证书的https express服务器)

以下是Postman的错误:
HTTPS请求永远挂起(带有自签名证书的https express服务器)

英文:

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?

Here is my code for the server:
HTTPS请求永远挂起(带有自签名证书的https express服务器)

Here is the postman error:
HTTPS请求永远挂起(带有自签名证书的https express服务器)

答案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.

huangapple
  • 本文由 发表于 2023年1月8日 16:35:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75046440.html
匿名

发表评论

匿名网友

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

确定