nodejs / help

:sparkles: Need help with Node.js? File an Issue here. :rocket:
1.47k stars 280 forks source link

Can't send post request on process.on("end", ...) #4282

Closed Skwrr closed 4 months ago

Skwrr commented 11 months ago

Details

I'm trying to make my nodejs app send a request to a nodejs web server to remove some details that only the nodejs app has. I tried using process.on("exit", ...) because when the app closes it gets fired, unfortunately it does not send the post request. Any idea of how to make it work?

Node.js version

v20.9.0

Example code

process.on("exit", async() => {
    const fetch = require("node-fetch")
    const response = await fetch("https://myserver.com:8080", {
        method: 'post',
        body: JSON.stringify({"message": "Client Disconnect", "ip": "deviceIp"}),
        headers: {'Content-Type': 'application/json'},
        keepalive: true
    })
    console.log(await response.json()) // I get nothing here, and the server does not receive the request
})
process.exit();

Operating system

Microsoft Windows [Versión 10.0.19045.3570]

Scope

Code, runtime

Module and version

node-fetch@2

github-actions[bot] commented 5 months ago

It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.

github-actions[bot] commented 4 months ago

It seems there has been no activity on this issue for a while, and it is being closed. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.