问题描述
这是我第一次在这里发帖,如有数据缺失,请见谅。
我正在尝试进行一些网络抓取以获取表格的一些信息。 该页面仅以 index.PHP 响应,当我使用搜索表单时,它会使用一些 formData 向 index.PHP?go=le 发送 POST。 为了避免 CORS 问题,我使用在本地主机中运行的我自己的 API 制作帖子。我将我的前端指向我的 API,我得到了来自 localhost 的响应。 没问题。
当我尝试向我的 API 发出第二个请求时,我的问题出现了。第一个 GET 工作正常,但在该响应之后它一直失败。
当我重新启动服务器时,它再次工作,但只有一次。
这是我的 API 代码。我使用 nodemon server.js
启动我的服务器。
server.js
const express = require("express");
const axios = require("axios");
const scrape = require("scrape-it");
const FormData = require("form-data")
const cors = require("cors")
const app = express();
const PORT = process.env.PORT || 5000;
app.use(cors())
const config = {
headers: {
'Content-type': 'multipart/form-data'
},}
app.get("/get-projects",async (req,res) => {
const testJSON = await axios.post(baseURL +"/index.PHP?go=le",formData,config)
.then(res => {
console.log("Post successfull...");
return res
}
)
.catch(err => {
console.log("Server error");
return err
}
);
if(testJSON && testJSON.data){
res.send({status: 200,data: testJSON.data});
}else{
res.status(508).send({status: 508,msg: "Unhandled Server Error",FailedResponse: testJSON || "empty"})
}
})
app.listen(PORT,()=>console.log(`App running in port: ${PORT}`))
在我的前端,我只有一个带有事件的按钮,可以访问我的 API (http://localhost:5000)
这是我的 fetch.js,包含在脚本标签中。那里没什么好看的。
fetch.js
const btn = document.getElementById("btn-fetch-proyects")
const axios = window.axios
const fetchProjects = async () => {
console.log("Fetching...")
axios.get("http://localhost:5000/get-projects")
.then(res=>
console.log("The server responded with the following data: ",res.data)
)
.catch(err => console.log("Failed with error: ",err)
)
return null
}
btn.addEventListener("click",fetchProjects);
在我运行服务器的控制台中,我用这个 Server error
对象得到 err
:
{
"message": "socket hang up","name": "Error","stack": "Error: socket hang up\n at connResetException (internal/errors.js:607:14)\n at Socket.socketonEnd (_http_client.js:493:23)\n at Socket.emit (events.js:327:22)\n at endReadableNT (internal/streams/readable.js:1327:12)\n at processticksAndRejections (internal/process/task_queues.js:80:21)","config": {
"url": "http://186.153.176.242:8095/index.PHP?go=le","method": "post","data": {
"_overheadLength": 1216,"_valueLength": 3,"_valuesToMeasure": [],"writable": false,"readable": true,"dataSize": 0,"maxDataSize": 2097152,"pauseStreams": true,"_released": true,"_streams": [],"_currentStream": null,"_insideLoop": false,"_pendingNext": false,"_boundary": "--------------------------935763531826714388665103","_events": {
"error": [
null,null
]
},"_eventsCount": 1
},"headers": {
"Accept": "application/json,text/plain,*/*","Content-Type": "multipart/form-data","User-Agent": "axios/0.21.1"
},"transformRequest": [
null
],"transformResponse": [
null
],"timeout": 0,"xsrfCookieName": "XSRF-TOKEN","xsrfheaderName": "X-XSRF-TOKEN","maxContentLength": -1,"maxBodyLength": -1
},"code": "ECONNRESET"
}
我希望有人知道发生了什么。试了一整天都解决不了。
我尝试发布到其他网站,它工作正常。我认为问题在于表单 POST。
感谢阅读!!!
解决方法
乍一看,我发现您的前端代码中有错误。您在函数上使用了 async
但随后您没有等待而是使用 .then
,请尽量不要混淆样式,无论您使用 async/await
还是 .then .catch.
检查是否有帮助! :)