Can i run nodejs command outside the terminal - javascript

Is there a way to run nodejs commands outside the terminal?
I mean, in order to run a nodejs command like { gulp watch }
I have to open nodejs command prompt and type gulp watch
Is there a way to run this command outside of the terminal from a web page, for example, by pressing the button to run the task directly
I want to build a site and use the great features provided by nodejs packages but the user will not use the command line
I'm looking for a way to do it with nodejs running in the background

It's unclear what you're asking:
Is there a way to run this command outside of the terminal from a web page
You don't need a terminal, for example you can run a node.js app in a window with electron.
I want to build a site and use the great features provided by nodejs packages but the user will not use the command line
A 'site' just transmits files to a browser, and browsers generally don't let web sites run code on a client's computer for security reasons, that would be insanity.
To have a public site that runs code on a client's computer, you would need some app outside your site the client would have to install, and communicate with the web site in some way.
A way to do this would be to have a client application that the user installs and that listens for requests from the server, generally termed an 'agent'. The agent would listen for requests for the server. The client's browser contacts the server and the server sends a request to the agent.
Another way would be to have the client app be a server itself, and connect using something like jsonrpc directly from the client's browser. Bitburner for example uses jsonrpc to connect to an app running on your local computer to sync files on your computer with files inside the game.
In both scenarios the client has to install and run an application outside the browser.

You can implement this like a online code compiler.
Suppose you have a button in the front end and you need to run some node.js stuffs, right?
Create a backend server (Nodejs or any other)
Write an api for accepting the request. This API will be called on button click.
if you want to run a static code for all button click, then keep the node.js program as a file in the server side.
Else accept the code snippet in the request body from the user, you will get snippet in the server side. Generate a file and save it.
Now you have the code snippet as a file in the server side. Make sure that, you are saving the file with proper extension (Like .js, .py etc).
In Node.JS, you can use exec from child_process to run the file from server side which result output.
You can return the output to user.
child_process.exec
For an example:
The user is not using command line in this method. Suppose you think about an online code compiler.
(The online compilers are webpages where you write your code and these codes are sent to the server which has the compiler built)
You select language there, python, cpp or nodejs.
Then you are typing your code in text area.
Click RUN button, What happens there?
It simply pass the selected language and code snippet written by user to the backend server.
In the backend, it may be in Python or java or Nodejs etc. Request will reach in server. From the request body we will get the code snippet write? In Node js , there is a exec method from child process, this method runs a command in a console and buffers the output.
This is purely a backend stuff and the user won't aware about what happening in the server.
Simply what this video, hope you will get the idea. Link also updated in answer.
Youtube link for online code compiler

Related

where should I install axios js? server side or client side?

I am trying to use axios for data loading. As I am new, i got confused where should I install axios js.I tried to install on client side. Is that alright?
Javascript files need to be hosted by a local (to the file invocation, not store) runtime engine. In a browser, for example, that might be the js v8 engine. Remotely, on a server, that server would have to have a local js runtime, presumably Node.js. The point is, if you want the js code to run in a browser, tag it in the html page, the browser will fetch the file from the web server, load it locally, and run the code. Otherwise, you an invoke it on the remote server, if the remote server has a runtime host such as Node, in the same way you invoke anything on a server; with a URI call (an example might theoretically be "https://......com/js-files/axios.js", and depending on if the server is configured to exec the resource, or fetch it, that's what it will do. Axios is isomorphic, so it can either run on the server in Node (if the server is correctly configured), or an HTML document can fetch it via a tag, and run the code locally, in the browser window/DOM. Is that clear?
You can do either or both! They are separate runtimes/environments.
According to the docs, Axios is a Promise based HTTP client for the browser and node.js. So it really depends on where you want to initiate the request.
If you want to do it on the browser consider adding the package in the HTML either via a script tag or a bundled js file:
See installation steps:
https://www.npmjs.com/package/axios#browser-support

How can I Execute NodeJS Server Client Side With Html Button Or Alternative If Not Possible?

Intro: Hello. So I started on a chat based social media app with Socket.io, but it seems like the client needs to open a localhost server to be able to communicate with the socket manager on my web API (node, express, socket.io).
Question Tree: Is it possible to make a nodejs server client side and execute a script with an html front end button so that the socket has a localhost to run tcp on to communicate to my web API? If so, how can I do this? If not, what are some alternatives to achieve the same results?
conclusion: Any help is appreciated, I am learning how to do this sort of development for school and I just want some clarity. Thank you!
It is possible if you are on linux you have to make a bash script file that will run your app.js or index.js node file, for ex
node app.js
that will get executed when you press a button on page which again is easy if you use some server side language like php if you are interested here is the link for moreenter link description here
with just html its not possible easily its just too much of a task, just type that command in your terminal and you are good to go
rather you can make a script like bash in linux or bat in windows, that will open the server client side if you open that script file.

How can I use Node.js Offline/locally?

Maybe i'm misunderstanding how Node.js works but, I would like to use it just as a server backend for a web app, without it running as a service/listening a port.
I'm willing to hear ideas to better solve the issue, this app will only be available on our intranet.
Example of what i'm thinking :
backend server.js :
function connectDb(usr, pwrd){
//Some npm package code to connect to a db
return console.log("Sucessfully connected")
}
frontend javascript.js :
require("server.js")
$(".connect.button").on("click", function(e){
connectDb($(".connect.user").text(), $(".connect.pwrd").text())
})
There are two different aspects with your question and code example on which you could work to get a better understanding of the ecosystem.
Client / Server
When a client wants to get some resource from a server, it connects to a specific port on that server, on which the back-end application is "listening". That means, to be able to serve resources coming from a database, you must have a Node process listening to a port, fetching the requested resources from the database, and returning them. The perfect format for that kind of data exchange is JSON.
To get a better understanding of this process, you may want to try and write a simple Node app sending a piece of JSON over the network when it receives a request, and try to load it with an XHR in client code (for example with JQuery's AJAX method). Then, try and serve a dynamic piece of JSON coming from a database, with a query based on the request's content.
Module loading
require("server.js") only works in Node, and can't be used in JavaScript that is running in a client's browser (Well, at least for now. Maybe some kind of module loading could be normalised for browsers, but that's another debate.).
To use a script in a client browser, you have to include it in the loaded page with a <script> tag.
In node, you can load a script file with require. However, said script must declare what functions or variables are exposed to the scripts that require it. To achieve it, you must export these variables or function setting module.exports.
See this article to get some basic understanding, and this part of Node docs to master all the details of module loading. This is quite important, as this will help you structure your app and avoid strange bugs.
For one thing, node itself isn't a web server: it's a JS interpreter, which (among other things) can be used to write a web server. But node itself isn't a web server any more than Java is.
But if you want things to be able to connect to your node program, in order to do things like access a database, or serve a webpage, then, yeah, your program needs to be listening on some port on the machine it's running on.
Simply having your node program listening to a specific port on your machine doesn't mean that anyone else can access it; but that's really a networking question not a programming question.

Getting a Python server to execute a script

I'm pretty new to web development, so bear with me if these questions seem pretty fundamental.
My use-case is as follows: I am hosting a server using ngrok which allows me to allow clients to securely tunnel to a server I host locally, using a public http(s) URL that ngrok generates.
Currently the server I am running locally is the simplest possible python web server (I literally just invoke 'python -m SimpleHTTPServer 8080').
Let's say I have a file "simplescript" located in the local directory where I instantiate the Python server. I would like to put in php code in "simplescript" so that when somebody fetches the file by using the browser to access "https://ngrokURL.ngrok.io/simplescript", then my server will execute the php code in the script directly and return the return value (if one is generated) to the client requesting my file.
Right now, when I try to do this, the client accessing my file only receives the actual text content of the script, and so it seems that the script is not being executed at all.
Question:
Is my use-case something that can be achieved using the Python simple HTTPserver, or do I need other tools?
How can I make my scripts executable for my server (if that is something I have to do)?
Thanks for the help!

executing a perl script on a remote machine as a root and getting the output back into the webpage using javascript

i perl script that reads the network traffic and prints out payloads of packets of specific length back onto the terminal. and for this to happen, the script should be initiated by root or else the packets will not be read.
i am using pcap in perl for that purpose.
now i have a webpage on my webserver. can i execute that perl script from my webpage and get the output that is being printed on the terminal into my webpage?
i am trying to use javascript to serve this purpose but i dont know how its done.
i tried googling it and all i got was ssh user#remote "perl pathto/perlfile.pl"
can someone help me out with this please?
To initiate a command from a web page, issue an AJAX call to another CGI script running on your webserver.
AJAX call can return the results of running your command (typically in JSON or XML format) and then call up some JavaScript handler to render that data in your page's DOM.
To run something as root on the web server's unix machine locally, you will need an suid script to be called from your CGI script mentioned in the first bullet (since web server and all the scripts it runs are typically run as non-root for security reasons).
To run something as root on ANOTHER machine, you have correctly found that you need to use ssh (from that same CGI script): ssh root#your_remotehost "perl pathto/perlfile.pl"
You will need to set up the ssh keys correctly between 2 systems so ssh doesn't ask for a password.
It is possible that you will NOT be able to ssh as root without a password when running as a non-root user; in which case you would need to:
ssh as the same user as your CGI script runs as
Have the command executed on remote server run the suid script, as described above for local case.
If you need further guidance, you will need to post specific code snippets and error messages or troubleshooting details so we can assess what the problems are.

Categories

Resources