Search code examples
javascriptnode.jsajaxnode-red

Import Ajax Library


I am new for node-red and node also. I have JS library which was used for jQuery project within GUI project. Now I want to build server side application using node-red. So for example I will have API like Login/Logout which will be called as HTTP methods. So I understand that I need to use HTTP In node to listen for POST. The questions:

  1. How to include my existing JS files as I did it in html:

    <script type="text/javascript" src="Includes/jQuery/jquery-2.0.3.js">/script>
    <script type="text/javascript" src="Includes/jQuery/jquery.xml2json.js"></script>
    <script type="text/javascript" src="Includes/jQuery/json2.js"></script>
    <script type="text/javascript" src="Includes/jQuery/json2xml.js">        </script>
    <script type="text/javascript" src="helpers.js"></script>
    <script type="text/javascript" src="Config.js"></script> 
    
  2. How to call function which are in above js libraries and create global object which will be available from Login Method till Logout method? for example in HTML, I did using global variables like below:

    <script type="text/javascript">
       var agent_ = null;  
       agent_ = new Agent(new AgentEventsHandler() new Logger());
    

Solution

  • 1) To make static files available to Node-RED web pages, you can simply configure a static folder.

    If you look in your settings.js file (normally found in ~/.node-red), you will see a commented out property called httpStatic, set this to a suitable folder, e.g.:

    httpStatic: path.join('', 'public'),
    

    which would use ~/.node-red/public on a standard installation.

    Your URL's would all be in the form: /images/myimage.png etc.

    2) To call a function in one of the libraries, you need to create a suitable page. Assuming you are using http-in/out, you would use the template node to create your page. Slightly trickier if you want to use the Dashboard. But not by much, in that case, you will use the Dashboard Template node.

    If you want to build more complex front-end apps, you might also want to look at node-red-contrib-uibuilder. This does the heavy lifting of creating folders and adding web resources along with managing the in/out messages between the back-end and the front-end.