-1

I have the following code:

const request = require("request");

const data = {
    "url": "http://example.net/",
    "lenght": "(30 * 1000)"
};

(async () => {

    const r = request.get(data.url);

    let chank = 0;
    r.on("data", (data) => {
        chank += data.length;
        console.log("data", Math.round(chank / 1024), "kb");
    });

    setTimeout(() => {
        r.abort();
    }, eval(data.lenght));

    const token = await get_token();

    let o = {
        formData: {
            file: {
                value: r,
                options: {
                    filename: "d",
                    contentType: "audio/mpeg"
                }
            },

            token: token
        },
        json: true
    };

    const u = await new Promise((resolve) => { // <----
        request.post("https://www.other.co.il/api/UploadFile", o, (err, res, body) => {
            resolve(body);
        });
    });

    console.log(u);


})();

The code downloads the file from Server A, and uploads it streaming to Server B. After some time, the download from Server A aborts, and Server B is supposed to return a response.

The problem: The code is seemingly ok, but what usually happens, the node.js stops (at the line marked with a arrow), without informing an error.

The following line is never executed. console.log(u);

The question is, what is wrong?

edit: This probably has to do with: https://stackoverflow.com/a/8636001/12054906

MusiCode7
  • 19
  • 3

1 Answers1

0

It looks like you're doing everything pretty much right, the question is, do you expect a body? If so, try logging out the body by not assigning the variable but rather logging the await new Promise bit. It shouldn't change the response, but if it does, your Promise is not grabbing that resolve. TLDR: you're probably just not getting a response from the other server. If you expect one, check the code on the server. A great way of sending requests without code would be to use a tool like Postman. That could be helpful with debugging as well.