# Sample flow for including JS file

**URL:** https://discourse.nodered.org/t/sample-flow-for-including-js-file/59454
**Category:** General
**Created:** [6 March 2022 20:15 UTC](https://discourse.nodered.org/t/sample-flow-for-including-js-file/59454 "2022-03-06T20:15:43Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![KevinT](https://avatars.discourse-cdn.com/v4/letter/k/f14d63/32.png) [@KevinT](https://discourse.nodered.org/u/KevinT)
#### Post date: [6 March 2022 20:15 UTC](https://discourse.nodered.org/t/sample-flow-for-including-js-file/59454/1 "2022-03-06T20:15:43Z")

</div>

I am trying to build a flow to generate and display an html-page with an included js files.

As this is on the edge of my capabilities and it took me a little effort to set up my docker system I would like to test it before digging into the real application.

So I set up a simple "hello world" flow. Could anyone help me with a very simple JS file that does just anything to show me if my setup works?

```auto
[{"id":"1ddb1a13.7a2ece","type":"http in","z":"66d2aa97.2a88b4","name":"/helloworld","url":"/helloworld","method":"get","upload":false,"swaggerDoc":"","x":810,"y":780,"wires":[["eec946da.dc757"]]},{"id":"c7a6dcd3.af8588","type":"inject","z":"66d2aa97.2a88b4","name":"Test","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":830,"y":820,"wires":[["eec946da.dc757"]]},{"id":"eec946da.dc757","type":"function","z":"66d2aa97.2a88b4","name":"Text","func":"\nmsg.helloworld = 'Hello World'\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":990,"y":780,"wires":[["b8c47711.60b948"]]},{"id":"b8c47711.60b948","type":"template","z":"66d2aa97.2a88b4","name":"Helloworld","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<!DOCTYPE html>\n<html>\n\n<head>\n <meta charset=\"utf-8\">\n <title>Hello World</title>\n</head>\n\n<body id=\"page-top\">\n <div id=\"wrapper\">\n <div id=\"content\">\n {{{helloworld}}}\n </div>\n </div>\n</body>\n <script src=\"/scripts/helloworld.js\"></script>\n</html>","output":"str","x":1150,"y":780,"wires":[["db257253.045928"]]},{"id":"db257253.045928","type":"http response","z":"66d2aa97.2a88b4","name":"","statusCode":"","headers":{},"x":1310,"y":780,"wires":[]}]

```

---

<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: [6 March 2022 20:39 UTC](https://discourse.nodered.org/t/sample-flow-for-including-js-file/59454/2 "2022-03-06T20:39:42Z")

</div>

Something very simple would be:

```auto
console.log('Hli there world')

```

You would see the output in the developer console of your browser.

If you want to send information back to Node-RED though, you will need a lot more. Of course, you could always use Dashboard or uibuilder. Dashboard builds a single page (with multiple tabs so it looks like several pages but really it isn't). With uibuilder, you put your HTML and JS in files and use the provided front-end library to enable 2-way communications with Node-RED.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [6 March 2022 20:49 UTC](https://discourse.nodered.org/t/sample-flow-for-including-js-file/59454/3 "2022-03-06T20:49:14Z")

</div>

There are a few ways to do this, you could serve up the js files from a sever or from a http static folder.  
You can also include the js direct in the template.  
e.g.

```auto
[{"id":"1ddb1a13.7a2ece","type":"http in","z":"bf9e1e33.030598","name":"/helloworld","url":"/helloworld","method":"get","upload":false,"swaggerDoc":"","x":200,"y":1540,"wires":[["eec946da.dc757"]]},{"id":"eec946da.dc757","type":"function","z":"bf9e1e33.030598","name":"Text","func":"\nmsg.helloworld = 'Hello World';\nmsg.js = `alert('hi')`;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":380,"y":1540,"wires":[["b8c47711.60b948"]]},{"id":"b8c47711.60b948","type":"template","z":"bf9e1e33.030598","name":"Helloworld","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<!DOCTYPE html>\n<html>\n\n<head>\n <meta charset=\"utf-8\">\n <title>Hello World</title>\n</head>\n\n<body id=\"page-top\">\n <div id=\"wrapper\">\n <div id=\"content\">\n {{{helloworld}}}\n </div>\n </div>\n <script>\n {{{js}}}\n </script>\n </body>\n\n</html>","output":"str","x":540,"y":1540,"wires":[["db257253.045928"]]},{"id":"db257253.045928","type":"http response","z":"bf9e1e33.030598","name":"","statusCode":"","headers":{},"x":700,"y":1540,"wires":[]}]

```

a simple example the shows a pop up.

---

<div class="post-metadata">

### Author: ![KevinT](https://avatars.discourse-cdn.com/v4/letter/k/f14d63/32.png) [@KevinT](https://discourse.nodered.org/u/KevinT)
#### Post date: [6 March 2022 21:07 UTC](https://discourse.nodered.org/t/sample-flow-for-including-js-file/59454/4 "2022-03-06T21:07:35Z")

</div>

thanks.  
I tried setting up a http static folder. To test this I would copy the single JS-command in a textfile and save it as helloworld.js - right?

... does not work - I seem to do something wrong accessing the static folder...

please help.  
I set the httpStatic as:

```auto
    functionGlobalContext: {
        // os:require('os'),
        // jfive:require("johnny-five"),
        // j5board:require("johnny-five").Board({repl:false})
		httpStatic:'/data'
    },

```

My docker is set up as:  
 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/a/eac57e8c94c189181d7333bca2b1e894fdf78d8c.png)

The http insert looks like:

```auto
    <script src="/scripts/helloworld.js"></script>

```

I copied the file here:  
 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/1/b171f89bf007c657578a7022a64701345ef7613b.png)

The content of the file is:

```auto
alert('hi')

```

Shouldn't this work?

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [6 March 2022 21:31 UTC](https://discourse.nodered.org/t/sample-flow-for-including-js-file/59454/5 "2022-03-06T21:31:56Z")

</div>

You have put `httpStatic` inside the `functionGlobalContext` setting. That is incorrect.

Your settings file should already have a commented out httpStatic example - find it, update it and uncomment it.

---

<div class="post-metadata">

### Author: ![KevinT](https://avatars.discourse-cdn.com/v4/letter/k/f14d63/32.png) [@KevinT](https://discourse.nodered.org/u/KevinT)
#### Post date: [6 March 2022 21:37 UTC](https://discourse.nodered.org/t/sample-flow-for-including-js-file/59454/6 "2022-03-06T21:37:54Z")

</div>

thanks.  
found it. changed it.  
still does not work

---

<div class="post-metadata">

### Author: ![KevinT](https://avatars.discourse-cdn.com/v4/letter/k/f14d63/32.png) [@KevinT](https://discourse.nodered.org/u/KevinT)
#### Post date: [7 March 2022 19:12 UTC](https://discourse.nodered.org/t/sample-flow-for-including-js-file/59454/7 "2022-03-07T19:12:25Z")

</div>

now it works.

working setup:

```auto
httpStatic:'/data/static'

```

mounted path

```auto
/data

```

scripts in:

```auto
[origin path]/scripts

```

html code

```auto
 <script src="/scripts/helloworld.js"></script>

```

---

<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: [6 May 2022 19:13 UTC](https://discourse.nodered.org/t/sample-flow-for-including-js-file/59454/8 "2022-05-06T19:13:11Z")

</div>

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