简体   繁体   English

sails.js如何构建实时Web控制台应用程序

[英]sails.js how build real time web console app

I'm new to sails.js, I only know the basics and am able to build simple MVC app. 我是sails.js的新手,我只了解基础知识,并且能够构建简单的MVC应用。 My boilerplate app: https://github.com/JerryYangJin/zero 我的样板应用程序: https : //github.com/JerryYangJin/zero

I want to use sails.js to build a real time web console app. 我想使用sails.js构建实时Web控制台应用程序。

The app works like this: 该应用程序的工作方式如下:

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.

Please advise, it would be great if there's open source example. 请告知,如果有开源示例,那就太好了。

Thanks in advance! 提前致谢!

Well, very simple code: simplest AJAX, no beaty. 好吧,非常简单的代码:最简单的AJAX,没有任何缺点。 Just example. 只是个例子。 Code is tested and fully workable. 代码已经过测试并且完全可以使用。

api/controllers/CliController.js API /控制器/ 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();
  }
};

Enter a command view views/cli/enter.ejs 输入命令查看 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>

Of course you MUST think about security. 当然,您必须考虑安全性。

I am using Node.js Child Process in the example. 我在示例中使用Node.js子进程

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM