Host web application in custom node

Hi folks,

We need to develop a new node that is able to host a web application.
The (third-party) web application consists out of a number of files:

image

Would like to build a custom node that allows users to start/stop this web application.
But not sure what the best way is to accomplish this.
Seems there are a number of options, based on some samples I found:

  • Via the http module:
    var http = require("http");  
    var server = http.createServer(function(request, response) {  
    response.writeHead(200, {  
    'Content-Type': 'text/plain'
    });  
    response.write("This is Test Message.");  
    response.end();  
    });  
    server.listen(8082);
    
  • Via express.js:
    var express = require("express");
    var app = express();
    app.use(express.static('public'));
    
    //make way for some custom css, js and images
    app.use('/css', express.static(__dirname + '/public/css'));
    app.use('/js', express.static(__dirname + '/public/js'));
    app.use('/images', express.static(__dirname + '/public/images'));
    
    var server = app.listen(8081, function(){
       var port = server.address().port;
    });
    
  • Others?

I have no idea how to get started. Moreover I assume Node-RED perhaps offers some functionality already to do this?

Thanks !!
Bart

Take a look at the Uibuilder source code for some inspiration: https://github.com/totallyinformation/node-red-contrib-uibuilder

Hey Lena (@afelix),
Is there any particular code snippet you are aiming at?

I want create a simple node that starts hosting the web application as soon as I send a start message to my node. So it should be small, stable and secure (??) ... And I don't want my users need to install anything manually ...

Bart, it might not be clear to people, this is for the design time environment (to be consumed by our node inside the node red editor environment), not some kind of dashboard or general purpose web server.

Steve, you are right ...
We want to host this web application, and then we will call it from our node's config screen (in the flow editor). So it indeed has nothing to do with the dashboard.

I understand Bart, what I mean is that Uibuilder is using Node-RED's own RED.httpNode Express Application.
https://nodered.org/docs/api/modules/v/0.20.0/node-red.html#.httpNode
That is what is used as HTTP server. Meaning that you can do the same, rather than create a full express installation at a different port, host a small application at its own endpoint within NR: https://github.com/TotallyInformation/node-red-contrib-uibuilder/blob/2161240559d3a18192ccdd1164c465cb8c1de7f9/nodes/uibuilder.js#L137

Okay based on what Steve is now adding that's going to be a bit harder... what I thought was having the Express app running at runtime, rather than at config time... Sounds like you would need to have indeed something start at a different port... but keeping that child process controlled and surviving control over different deploys sounds extremely hard. Far above my levels if you need to support more than just POSIX systems.

Might be a bit off topic (sorry).

What I would like to do is incorporate some 'html code' within Node-RED to create some web pages.

I've created quite a few web sites (in a previous life) and it would be nice to combine a simple web page navigation and images within Node-RED.

Any ideas or links to practical examples??

@dynamicdave - If they are simple html pages can't you just host them as static pages using the httpStatic setting path in settings.js ?

@BartButenaers If this is for use in the editor then you should probably add it as an express path within the admin tree path so that it is protected by the admin ui settings.

2 Likes

Thanks for your swift responses - I'll give that a try on Monday.

I just needed something simple for my IoT students and me !!!

Hey Dave,
I assume you mean using simply RED.httpAdmin.get? Damn, should have thought about that myself ...

But there is some extra complexity in our case: from our node's config screen we open the URL via an iframe. So the iframe needs to access to my local web application. Is that possible with your solution, or should we use something else then?

1 Like

RED.httpAdmin is a reference to the ExpressJS app. get, put, etc. add a route for that specific request type. You will see examples in uibuilder towards the end of the uibuilder.js file where I create a number of API's.

However, don't forget that you need to destroy any extra paths you create when unloading or you will find that you end up with multiple references and a memory leak. the processClose() function in uilib.js shows you how.

In your case, you may need to remove the path when stopping otherwise, I'm not sure how you will actually stop the "server". Though I suppose you could simply make the request return a 404 or a redirect. That wouldn't work if you simply use the serveStatic feature though.

For security, I assume you will be relying on the Node-RED editor security features? You will want to set up https as well of course.

1 Like