3

こちらに質問するのは初めてなので、情報が不足していたらすみません。

テーブルの情報を取得するために、 Webスクレイピングを実行しようとしています。ページは index.php でのみ応答し、検索フォームを使用すると、formData を使用して index.php?go=le に POST が送信されます。CORS の問題を回避するために、localhost で実行されている独自の API を使用して投稿を作成しています。フロントエンドを API に向けると、localhost から応答が返ってきます。問題ありません。

API に 2 番目のリクエストを送信しようとすると、問題が発生します。最初の GET は正常に機能しますが、その後は失敗し続けます。
サーバーを再起動すると、再び機能しますが、1回だけです。

これが私のAPIコードです。サーバーを起動するために使用nodemon server.jsします。

サーバー.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) にアクセスするイベントを持つボタンしかありません。

これは、script タグによってインクルードされた私の 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 にあると思います。

読んでくれてありがとう!!!

4

1 に答える 1