【问题标题】:Client Side, to API, POST http... net::ERR_CONNECTION_TIMED_OUT客户端,到 API,POST http... net::ERR_CONNECTION_TIMED_OUT
【发布时间】:2020-05-27 17:46:44
【问题描述】:

我做了一个基本的 https 响应,将 JSON 格式的数据从客户端发送到我的 a2 Web 服务器上名为 /api 的特定端点的 API。由于某种原因,连接被拒绝,我通过 SSH 访问的终端最终没有记录任何内容。

服务器端代码:

const express = require('express');
const app = express();
app.listen(3000,()=>console.log('listening on port 3000'));
app.use(express.static('public'));

app.post('/api',(request,response)=>
{
    console.log(request);
});

要向客户端提供的页面:

 <!DOCTYPE html>

<html>
    <head>

    </head>
    <body>
        <script>
        console.log("Page started");
            const num1=1;
            const num2=2;
            const data=35;//{num1,num2};
            const options =
            {
                method: 'POST',
                headers:
                {
                    "Content-Type": "application/json"
                },
                body: JSON.stringify(data)
            };
            fetch(/*'https://goldengates.club:3000/api'*/'https://goldengates.club:3000/api',options);
        </script>
    </body>
</html>

提供的客户端页面上的错误消息:

Page started
(index):22 POST https://goldengates.club:3000/api net::ERR_CONNECTION_TIMED_OUT
(anonymous) @ (index):22
goldengates.club/:1 Uncaught (in promise) TypeError: Failed to fetch

我知道存在重大安全漏洞,这只是为了熟悉我从未使用过的特定功能。

【问题讨论】:

    标签: javascript node.js http backend web-development-server


    【解决方案1】:

    在客户端更改 API 基本 URL 中,而不是 https://goldengates.club:3000/api 使用下面给出的:

    https://goldengates.club/api
    

    请试试这个。 HTTP... net:: ERR_CONNECTION_TIMED_OUT 错误:原因是您的请求 URL 无法找到路径。我们不需要使用域名 + 端口 (https://goldengates.club:3000)

    【讨论】:

    • 感谢您的回复,但这是不正确的。服务器端在 3000 端口上运行,需要指定。它也给了我错误 404:(index):22 POST https://goldengates.club/api 404
    • 请您检查您的域是否正确指向您的服务器端代码而不是服务器代码路径,例如 cd /root/home/projects/server/ProjectFolder 检查该域是否指向此特定路径并收听这个端口与否。错误原因是未找到请求的 URL。因为您的域无法指向您特定的文件夹路径
    • 请在 API 方法中添加 Response : response.setHeader("Content-Type", "application/json");response.send({ statusCode:200, customMessage : 'Sucess',状态:真})
    • 所以你想让我为服务器端代码放置正确的目录吗? goldengates.club/Cypher-Network/fetchTest.js ?我为 API 制作的用于接收请求的端点呢?
    • 我们不需要使用域 + 端口,因为我们的域已经在特定端口上运行。请不要介意,但您可以检查没有人使用域名 + PORT 的任何网站。谢谢:)
    【解决方案2】:

    您没有在您的快速申请中正确结束请求。您需要拨打response.end();

    【讨论】:

    • 我应该始终发送回复或结束回复,是的,但问题仍然存在,不过感谢您的回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-24
    • 1970-01-01
    • 1970-01-01
    • 2016-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多