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:
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?
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.
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.
@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.
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?
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.