Home > Software engineering >  Can i run nodejs command outside the terminal
Can i run nodejs command outside the terminal

Time:01-18

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

CodePudding user response:

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.

CodePudding user response:

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)

Right 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

  • Related