# Response handling from External NPM Module

**URL:** <https://discourse.nodered.org/t/response-handling-from-external-npm-module/87795>\
**Category:** General\
**Tags:** function-node, external-package, async, module\
**Created:** [7 May 2024 19:06 UTC](https://discourse.nodered.org/t/response-handling-from-external-npm-module/87795 "2024-05-07T19:06:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![VinistoisR](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vinistoisr/32/5778_2.png) [@VinistoisR](https://discourse.nodered.org/u/VinistoisR)\
**Post date:** [7 May 2024 19:06 UTC](https://discourse.nodered.org/t/response-handling-from-external-npm-module/87795/1 "2024-05-07T19:06:04Z")

</div>

In a Function Node, I added this NPM module in the setup tab:

> **[GitHub - huksley/prometheus-remote-write: Send samples to prometheus via...](https://github.com/huksley/prometheus-remote-write)**
>
> Send samples to prometheus via remote\_write from NodeJS - huksley/prometheus-remote-write

It's working with the following function code, adapted from the module's readme:

```auto
// Send "UP" Heartbeat to Prometheus endpoint using Prometheus-Remote-Write NPM module
// https://www.npmjs.com/package/prometheus-remote-write
// Module imported as prometheusRemoteWrite in Setup Tab
// incoming msg.payload = 1

const { pushTimeseries, pushMetrics } = prometheusRemoteWrite;

const config = {
    url: "http://victoriametrics_vmagent:8429/api/v1/write",
    verbose: true, // Enable verbose logging
    timing: true // Enable response timing data
};

async function Push() {
    try {
        node.status({ fill: "blue", shape: "dot", text: "connecting" });
        await pushTimeseries({
            labels: {
                __name__ : "up",
                instance: "nodered.domain.com",
                job: "node-red"
            },
            samples: [{
                value: msg.payload,
                timestamp: Date.now()
            }]
        }, config);
  
        msg.result = "Data push successful";
        node.status({fill:"green", shape:"dot", text:"connected"});
        node.send(msg);
        
    } catch (error) {
        node.error("Error encountered during data push:", error);
        node.status({fill:"red", shape:"ring", text:"Error"});
    }
}

Push();

```

On the container's console, I get this return:

```auto
Failed to send write request, error 204 No Content { timeseries: [{ labels: [Array], samples: [Array] } ] }
Serialized in 1 ms

```

The status code 204 is being returned although the data is written into the database without issue.

The relevant line in the module is [here](https://github.com/huksley/prometheus-remote-write/blob/63a391dfcd6daf12e013acf3bf7d6288b79440af/index.js#L112):

` logger.warn("Failed to send write request, error", r.status + " " + r.statusText + " " + text, writeRequest);`

I don't know why it's sending this response when the data is being written successfully?

Looking at the code, it almost seems as though it would only return 200 if you send it nothing to write. I was hoping for confirmation that the write indeed occurred.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [7 May 2024 19:17 UTC](https://discourse.nodered.org/t/response-handling-from-external-npm-module/87795/2 "2024-05-07T19:17:54Z")

</div>

> [@VinistoisR](#):
>
> The status code 204 is being returned although the data is written into the database without issue.

In general, a 200 status code is used for most types of successful requests, while a 204 status code is used when the server has processed the request successfully, but there is no content to return to the client

Basically, 204 == "GOOD"

---

<div class="post-metadata">

**Author:** ![VinistoisR](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vinistoisr/32/5778_2.png) [@VinistoisR](https://discourse.nodered.org/u/VinistoisR)\
**Post date:** [7 May 2024 19:37 UTC](https://discourse.nodered.org/t/response-handling-from-external-npm-module/87795/3 "2024-05-07T19:37:24Z")

</div>

Thanks! that's my understanding as well, but the module is returning "Failed to send write request, error" for any non-200 response code.

---

<div class="post-metadata">

**Author:** ![VinistoisR](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vinistoisr/32/5778_2.png) [@VinistoisR](https://discourse.nodered.org/u/VinistoisR)\
**Post date:** [7 May 2024 20:40 UTC](https://discourse.nodered.org/t/response-handling-from-external-npm-module/87795/4 "2024-05-07T20:40:04Z")

</div>

it was a bug, the dev fixed it with this commit:

> <https://github.com/huksley/prometheus-remote-write/commit/f38484f368575268e68ca2d1c48bbec41870e97e>

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [21 May 2024 20:40 UTC](https://discourse.nodered.org/t/response-handling-from-external-npm-module/87795/5 "2024-05-21T20:40:40Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
