3

Im new to node.js and this is my question:

For example: I got web application and from that application I have a button and on button click I want to run node console command (for example: node socket.io).

So:

$( ".button" ).on( "click", function() {
   //run ajax to node with command for example "node socket.io"
 });

So in this example I want to start socket.io from web javascript file. How to do it ? I know its very basic question but I want to understand how to do it or is it even possible.

EDIT

But is it possible to run ajax request with that node command to node.js and then fire up it up ("node socket.io")?

Im asking this because I want to start and stop socket.io from web application, not from console command directly.

4
  • As nodejs runs on the server, you can't achieve this by running a command on the client. Think of nodejs as a replacement for php or RoR, or some other backend technology. You need to set up a socket connection between client and server and then fire events that are listened to on both ends. Commented Jan 27, 2016 at 10:43
  • But is it possible to run ajax request to node.js and then fire up a node command ("node socket.io")? Commented Jan 27, 2016 at 10:47
  • 1
    Yes, it's possible, but you'd first need to start a server that receives this request (e.g. node remote-start.js) anyway, so why not just start socket.io directly? Commented Jan 27, 2016 at 11:56
  • I thought of that to save server resources (cpu/ram). So when I need socket.io functionality I just start it form a web app interface and when I don't need it i can turn it off from a web app interface as well . Maybe I am lacking some knowledge and my thinking about node + socket.io application is not correct - that way Im asking that kind of question :) Commented Jan 27, 2016 at 12:35

1 Answer 1

4

You would need an express route like this:

...

var exec = require('child_process').exec;

app.post('/exec', function(req, res) {
  var cmd = req.body.command;

  exec(cmd, function(error, stdout, stderr) {
    if (stderr || error) {
      res.json({
        success: false,
        error: stderr || error,
        command: cmd,
        result: null
      })
    } else {
      res.json({
        success: true,
        error: null,
        command: cmd,
        result: stdout
      })
    }
  })


})

...

note: stderr and stdout are buffers.

You then need to POST your command (using AJAX or a form) to /exec. Which will give you a response such as:

Success:

{
  success: true,
  error: null,
  command: "ls",
  result: "app.js bin node_modules package.json public routes views "
}

Failure:

{
    success: false,
    error: "/bin/sh: foobar: command not found ",
    command: "foobar",
    result: null
}

You need to be extremely careful with security having something like this though as you are opening up access to your system's console.

Sign up to request clarification or add additional context in comments.

4 Comments

This suppose to be available to only one user - super admin.
Still, it's considerably harder to access a system secured using ssh than it is to access an admin page. You need to make sure it's secured to attacks such as csrf. If this answer is correct, make sure you mark it as a solution, thanks.
I will be aware of that. Really thank you for your answer. I will test it at and then mark it as correct answer :).
I've updated my answer to respond with JSON, which is probably easier to work with if you're using AJAX. Also, on my initial answer I used req.param.command which should have been req.body.command, and the route should have just been /exec, not /exec/:command. These changes are reflected in my edit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.