sails.js 如何构建实时 Web 控制台应用程序
sails.js how build real time web console app
我是 sails.js 的新手,我只了解基础知识并且能够构建简单的 MVC 应用程序。我的样板应用程序:https://github.com/JerryYangJin/zero
我想使用 sails.js 构建实时 Web 控制台应用程序。
应用程序是这样工作的:
1. user types command in web browser
2. sails(socket.io) to call a native console program on the server, using user typed command as STDIN
3. sails(socket.io) to get native console program output from STDOUT then send it back to user.
请指教,如果有开源的例子就好了
提前致谢!
好吧,非常简单的代码:最简单的 AJAX,不多说了。只是例子。代码已经过测试并且完全可用。
api/controllers/CliController.js
module.exports = {
/**
* Command execution
* @param {String} req.body.command [command ro execute]
* @return {JSON} [result or error]
*/
run: function (req, res) {
// Check for valid command string in request
if (req.body === undefined || !req.body.command || req.body.command.trim().length === 0){
return res.redirect('/cli/enter');
}
// Split string to array as `child_process` needs array
var command = req.body.command.split(" "),
err = [],
result = "";
// Get needed module and execute command
var spawn = require('child_process').spawn,
run = spawn(command.splice(0, 1)[0], command);
run.stdout.on('data', function (data) {
result = result + data.toString();
});
run.stderr.on('data', function (data) {
err.push(data.toString());
console.log(data);
});
run.on('error', function (error) {
err.push(error.toString());
console.log(error);
run.stdin.end();
});
run.on('close', function (code) {
if (code !== 0) {
err.push('ps process exited with code ' + code);
}
run.stdin.end();
// Return error
if (err.length > 0){
res.status(500);
return res.json({errors: err});
}
//Return data
return res.ok(result);
});
},
/**
* Simple function to render view with command line
*/
enter: function(req, res){
return res.view();
}
};
进入命令视图 views/cli/enter.ejs
<input type="text" name="command" id="command"/>
<button id="sendCommand">Execute</button>
<pre id="result"></pre>
<script type="text/javascript">
$('#sendCommand').click(function(){
var command = $('#command').val().trim();
if (command.length > 0){
io.socket.post('/cli/run', {command: command}, function(data, jwr){
if (jwr.statusCode == 200){
$('#result').text(data);
} else {
$('#result').text('ERROR: ' + jwr.statusCode + JSON.stringify(data.errors));
}
});
}
});
</script>
当然,您必须考虑安全性。
我在示例中使用 Node.js Child Process。
希望对您有所帮助。
我是 sails.js 的新手,我只了解基础知识并且能够构建简单的 MVC 应用程序。我的样板应用程序:https://github.com/JerryYangJin/zero
我想使用 sails.js 构建实时 Web 控制台应用程序。
应用程序是这样工作的:
1. user types command in web browser
2. sails(socket.io) to call a native console program on the server, using user typed command as STDIN
3. sails(socket.io) to get native console program output from STDOUT then send it back to user.
请指教,如果有开源的例子就好了
提前致谢!
好吧,非常简单的代码:最简单的 AJAX,不多说了。只是例子。代码已经过测试并且完全可用。
api/controllers/CliController.js
module.exports = {
/**
* Command execution
* @param {String} req.body.command [command ro execute]
* @return {JSON} [result or error]
*/
run: function (req, res) {
// Check for valid command string in request
if (req.body === undefined || !req.body.command || req.body.command.trim().length === 0){
return res.redirect('/cli/enter');
}
// Split string to array as `child_process` needs array
var command = req.body.command.split(" "),
err = [],
result = "";
// Get needed module and execute command
var spawn = require('child_process').spawn,
run = spawn(command.splice(0, 1)[0], command);
run.stdout.on('data', function (data) {
result = result + data.toString();
});
run.stderr.on('data', function (data) {
err.push(data.toString());
console.log(data);
});
run.on('error', function (error) {
err.push(error.toString());
console.log(error);
run.stdin.end();
});
run.on('close', function (code) {
if (code !== 0) {
err.push('ps process exited with code ' + code);
}
run.stdin.end();
// Return error
if (err.length > 0){
res.status(500);
return res.json({errors: err});
}
//Return data
return res.ok(result);
});
},
/**
* Simple function to render view with command line
*/
enter: function(req, res){
return res.view();
}
};
进入命令视图 views/cli/enter.ejs
<input type="text" name="command" id="command"/>
<button id="sendCommand">Execute</button>
<pre id="result"></pre>
<script type="text/javascript">
$('#sendCommand').click(function(){
var command = $('#command').val().trim();
if (command.length > 0){
io.socket.post('/cli/run', {command: command}, function(data, jwr){
if (jwr.statusCode == 200){
$('#result').text(data);
} else {
$('#result').text('ERROR: ' + jwr.statusCode + JSON.stringify(data.errors));
}
});
}
});
</script>
当然,您必须考虑安全性。
我在示例中使用 Node.js Child Process。
希望对您有所帮助。