【发布时间】:2020-05-27 20:00:33
【问题描述】:
我的 api 没有将数据从客户端代码获取到我在端口 3000 上的网络服务器的 localhost 上创建的 /api 端点。如何获取它以获取服务器端代码以获取发布请求和控制台。日志(请求);如果它是正确的Page started
(index):23 POST https://goldengates.club:3000/api net::ERR_CONNECTION_TIMED_OUT
(anonymous) @ (index):23
goldengates.club/:1 Uncaught (in promise) TypeError: Failed to fetch
客户端代码:
<!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/Cypher-Network/fetchTest.js'*/,options);
</script>
</body>
</html>
服务器端代码:
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)=>
{
response.setHeader("Content-Type", "application/json");response.send({ statusCode:200, customMessage : 'Sucess', status : true })
console.log(request);
response.end();
});
【问题讨论】:
标签: javascript node.js api http-post backend