# Send data from custom node node.js script to html/browser?

**URL:** <https://discourse.nodered.org/t/send-data-from-custom-node-node-js-script-to-html-browser/80838>\
**Category:** Developing Nodes\
**Created:** [27 August 2023 10:36 UTC](https://discourse.nodered.org/t/send-data-from-custom-node-node-js-script-to-html-browser/80838 "2023-08-27T10:36:36Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![FezVrasta](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fezvrasta/32/2182_2.png) [@FezVrasta](https://discourse.nodered.org/u/FezVrasta)\
**Post date:** [27 August 2023 10:36 UTC](https://discourse.nodered.org/t/send-data-from-custom-node-node-js-script-to-html-browser/80838/1 "2023-08-27T10:36:36Z")

</div>

Hi, I'm writing a custom node plugin and I would like to display in the HTML form page some data that's available in the node.js side of the script. I can't really understand how to do that and I can't find any documentation, could someone point me in the right direction please?

To give some context, I'm generating a setup code and I need to display it on the Node-RED ui to allow the user to use it.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [27 August 2023 11:31 UTC](https://discourse.nodered.org/t/send-data-from-custom-node-node-js-script-to-html-browser/80838/2 "2023-08-27T11:31:38Z")

</div>

You have a couple of choices. Of which the "official" approach would be to create an API end-point on the admin instance of ExpressJS that is exposed to your runtime code on `RED.httpAdmin`. You can attach a new endpoint (or an ExpressJS router with its own endpoints) to that which will return the data you need. You call it from within the JavaScript of your html file.

The core serial node has a simple example. For more complex examples, you can wade through the uibuilder code. All of the web processing in uibuilder starts from the UibWeb class in `libs/web.js`.

---

<div class="post-metadata">

**Author:** ![FezVrasta](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fezvrasta/32/2182_2.png) [@FezVrasta](https://discourse.nodered.org/u/FezVrasta)\
**Post date:** [27 August 2023 11:58 UTC](https://discourse.nodered.org/t/send-data-from-custom-node-node-js-script-to-html-browser/80838/3 "2023-08-27T11:58:23Z")

</div>

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![FezVrasta](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fezvrasta/32/2182_2.png) [@FezVrasta](https://discourse.nodered.org/u/FezVrasta)\
**Post date:** [27 August 2023 12:44 UTC](https://discourse.nodered.org/t/send-data-from-custom-node-node-js-script-to-html-browser/80838/4 "2023-08-27T12:44:12Z")

</div>

@TotallyInformation I'm getting a `Cannot GET /my-path` (404 error) when I register the endpoint with:

```auto
 RED.httpAdmin.use(
    '/my-path/:id',
    function (req, res) {
      res.send({ hello: "World!");
    }
  );

```

Do you have any suggestion? I also tried` httpAdmin.get`

I'm fetching it with

```auto
      $.getJSON('my-path/123', function (data) {
        console.log(data);
      });

```

And I see it correctly (?) calls `http://127.0.0.1:1880/my-path`

**Edit** it seems to be a problem with params (`:/foo`), if I use query arguments it works 🤷‍♂️

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [27 August 2023 12:58 UTC](https://discourse.nodered.org/t/send-data-from-custom-node-node-js-script-to-html-browser/80838/5 "2023-08-27T12:58:43Z")

</div>

Should work I think. if you manually set the url in the browser to `http://127.0.0.1:1880/my-path/abc`, does that work?

You probably want to use `RED.httpAdmin.get` rather than `use`, just so it is clear you only ever want to use a GET on that endpoint.

I always use query arguments on GET REST API's. If I need something more complex, I'll use POST with a body containing JSON. You should always use POST if needing to send anything sensitive (and use HTTPS of course). I have a helper function that puts all parameters no matter what form they come in into a standard object.

---

<div class="post-metadata">

**Author:** ![FezVrasta](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fezvrasta/32/2182_2.png) [@FezVrasta](https://discourse.nodered.org/u/FezVrasta)\
**Post date:** [27 August 2023 13:03 UTC](https://discourse.nodered.org/t/send-data-from-custom-node-node-js-script-to-html-browser/80838/6 "2023-08-27T13:03:27Z")

</div>

Nope, not working, so weird. Not a big deal but it's the first time I see Express act like this.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [27 August 2023 13:29 UTC](https://discourse.nodered.org/t/send-data-from-custom-node-node-js-script-to-html-browser/80838/7 "2023-08-27T13:29:40Z")

</div>

Just make sure that whatever base URL path you chose, it won't ever clash with anything else (so don't use a common word for example).

For uibuilder, everything starts with `/uibuilder/...`

---

<div class="post-metadata">

**Author:** ![FezVrasta](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fezvrasta/32/2182_2.png) [@FezVrasta](https://discourse.nodered.org/u/FezVrasta)\
**Post date:** [27 August 2023 13:33 UTC](https://discourse.nodered.org/t/send-data-from-custom-node-node-js-script-to-html-browser/80838/8 "2023-08-27T13:33:19Z")

</div>

I used my package name as scope so it should be good.

---

<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:** [27 August 2023 13:51 UTC](https://discourse.nodered.org/t/send-data-from-custom-node-node-js-script-to-html-browser/80838/9 "2023-08-27T13:51:49Z")

</div>

> [@FezVrasta](#):
>
> `RED.httpAdmin.use`

Any reason you chose `use` instead of a specific verb?

> [@FezVrasta](#):
>
> Nope, not working,

Here is a working example.

> [@Using a client side JS file](https://discourse.nodered.org/t/using-a-client-side-js-file/47338/4):
>
> There are a few ways to achieve this but ultimately you have to serve the code via an endpoint. Here is an example from one of my nodes - I like this pattern as I can share the js with both client and server side. In your project, add folders /static/, /static/js/ & /static/css/ Add a "dumb" node-red node called (for example) app.js - this is the end point that serves files from /static/ to http://xxxxx/image\_tools/static/\*\*\* module.exports = function (RED) { /\*\* \* Enable http route …

---

<div class="post-metadata">

**Author:** ![FezVrasta](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fezvrasta/32/2182_2.png) [@FezVrasta](https://discourse.nodered.org/u/FezVrasta)\
**Post date:** [28 August 2023 11:41 UTC](https://discourse.nodered.org/t/send-data-from-custom-node-node-js-script-to-html-browser/80838/10 "2023-08-28T11:41:54Z")

</div>

@Steve-Mcl as I mentioned I also tried with `.get`.

For now I got it working with a query parameter so I'll leave it as is, thanks for the help though!

---

<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:** [27 October 2023 11:42 UTC](https://discourse.nodered.org/t/send-data-from-custom-node-node-js-script-to-html-browser/80838/11 "2023-10-27T11:42:12Z")

</div>

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