A little advice please on node.js

Hi I'm just trying to understand node.js and wondering if anyone can please point me in the direction of some learning material.

From a html button I want to call an .sh script that would grep/sed/awk a 3rd party html page and output a simple html page response. Simply just to indicate that the site is up or down.

But to do this I believe I would have to use javascript that calls node.js to run the .sh script.

Is that correct so far?

All you need is "just" a web server with script interpreter support.
For example lighttpd with node.js if you want to write your script in js.
(Substitute lighttpd with your favourite web server)
More traditionally lighttpd with php would be used.
I am pretty sure php is much smaller than node.js and lighttpd is hard enough for exposure to raw Internet and also has a small memory footprint.
Your choice, depending on the resources of the router you are running on.

1 Like

Can't I just use node on OpenWrt?

"Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js' package ecosystem, npm, is the largest ecosystem of open source libraries in the world.
Require approx. 5.5 MB size for 5 package(s) to install."

You definitely do NOT need node.js to run the shell script. Also, you don't need the non-standard web-server either, what you're talking about can be accomplished with the uhttpd which is default server.

1 Like

Thankyou @stangri that sounds much better. I have a second site running on uhttpd configured by luci-app-uhttpd and just need a button on there to query another site. The other site goes offline occasionally (most likely maintenance). When it's online it shows a Windows IIS page and offline is Cloudflare so it's a pretty simple thing for me to grep into a shell script.

Could you please provide a simple example of how to call the script?

Thank you

I'm sure you can adapt the fakeinternet code, just make sure it outputs the proper HTML with the button when called with no parameters and if invoked with a specific parameter (link from the button) it parses the target and outputs the result.

Um yeah idk. I'm not at all familiar with your package or how it even works. My OpenWrt device is not performing any router functions. It operates simply as a static device on the network running mainly uhttdp and rsync backups.

Could you please offer anything further in relation to how I would use this?

Node.js is a Javascript interpretor that runs on a server as apposed to the Javascript interpretor that runs in a browser. This means you can run Javascript code on a server ( usually it is run in the user's browser ).

What you want to do though is very simple and you already have (most likely) a [b]ash script to do it.

Don't overthink the problem with node.js.

I am pretty sure you can run your ash script as uhttpd/cgi (or at least a version of it).

See this for example:

1 Like

Ok cool thanks @bluewavenet so I need to learn about cgi instead.

I'll give it a go