Hopefully the title says it all but when developing custom nodes whats the recommended approach to serving static content in say ./docs? I know httpStatic can be used in settings.js but thats locally set right? Is there a recommended way to dynamically update this?
Thanks, I'm using that for static content such as images etc, but thought there might have been a way to have a cleaner URL ie. https://noderedurl/documentation
You can attach your own routes to Node-RED's ExpressJS server if you want to. Just remember to remove them in the close callback otherwise you can end up with multiple instances.
Thanks, with a bit of testing I've managed to figure this out, might not be the most elegant of solutions but here is what works for me:
// grab the node express instance
const server = RED.httpNode;
try {
// set a route to /docs and handle subdirs
server.get('/docs/:resourcePath(*)', function (req, res) {
// handle default file ie index.html
const resourcePath = req.params.resourcePath || 'index.html';
const filePath = path.join(__dirname, '../docs', resourcePath);
// Determine the content type based on the file's extension
const contentType = mime.lookup(filePath);
// Read the data from the file
fs.readFile(filePath, (err, data) => {
if (err) {
// Handle errors, e.g., file not found
res.status(404).send('File not found');
} else {
// Set the content type and send the data in the response
res.set('Content-Type', contentType);
res.send(data);
}
});
});
} catch (error) {
console.log(error);
}
I mean if you really needed to do it this way.
Maybe use a prefix to the URI or something to partition it away from other things.
Example: before I started using Resources with my ZWave Node I used to do: server.get('/zwjs/api/:call', function (req, res) {}) server.get('/zwjs/docs/device/:part', function (req, res) {}) server.get('/zwjs/docs/controller/:part', function (req, res) {})