admin管理员组文章数量:1418104
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 mand (for example: node socket.io).
So:
$( ".button" ).on( "click", function() {
//run ajax to node with mand 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 mand 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 mand directly.
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 mand (for example: node socket.io).
So:
$( ".button" ).on( "click", function() {
//run ajax to node with mand 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 mand 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 mand directly.
Share Improve this question edited Jan 27, 2016 at 11:16 born2fr4g asked Jan 27, 2016 at 10:38 born2fr4gborn2fr4g 1,3004 gold badges27 silver badges40 bronze badges 4- As nodejs runs on the server, you can't achieve this by running a mand 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. – Stevo Commented Jan 27, 2016 at 10:43
- But is it possible to run ajax request to node.js and then fire up a node mand ("node socket.io")? – born2fr4g 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? – Bergi 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 :) – born2fr4g Commented Jan 27, 2016 at 12:35
1 Answer
Reset to default 4You would need an express route like this:
...
var exec = require('child_process').exec;
app.post('/exec', function(req, res) {
var cmd = req.body.mand;
exec(cmd, function(error, stdout, stderr) {
if (stderr || error) {
res.json({
success: false,
error: stderr || error,
mand: cmd,
result: null
})
} else {
res.json({
success: true,
error: null,
mand: cmd,
result: stdout
})
}
})
})
...
note: stderr and stdout are buffers.
You then need to POST
your mand (using AJAX or a form) to /exec
. Which will give you a response such as:
Success:
{
success: true,
error: null,
mand: "ls",
result: "app.js bin node_modules package.json public routes views "
}
Failure:
{
success: false,
error: "/bin/sh: foobar: mand not found ",
mand: "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.
本文标签: nodejsHow to execute node (console) command from javascript file on click via ajaxStack Overflow
版权声明:本文标题:node.js - How to execute node (console) command from javascript file on click via ajax - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745209328a2647789.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论