angularjs - 在 Express 和 Angular Controller 中与其他路线共享子进程的结果

标签 angularjs node.js express child-process

所以我正在使用 Node.js、Express.js 和 Angular.js。我想要做的是获取子进程的结果,将其传递给路由,然后让 Angular 执行 get 请求来获取该数据。

我环顾四周,但我看到的大多数答案都是关于数据库连接和请求的。

我要 POST 的路由只是普通的 Express.js 路由。

这是我的代码: 索引.js:

var express = require('express');
var router = express.Router();
var spawn = require('child-process-promise').spawn;
var data = '';

/* GET  */
router.get('/', function(req, res, next) {
res.render('index');
   });

 /* POST  */
router.post('/', function(req, res, next) {

/* Arguements from the clients request body */

/* Child process to run python scripts wrapped in Promise to 
run the scripts in a synchronous form
 */

/* Script and taking in arguments*/
spawn('python',["public/model/model.py",'input1','input2']).progress(function(childProcess){

    /* Response to the data that is received */
        childProcess.stdout.on('data',function(data){
            data = data.toString();
        });
        /* Prints Error Message */
        childProcess.stderr.on('data',function(data){
            console.log(data.toString());
        });

    }).fail(function(err){
        console.error(err);
    });

   });

  module.exports = router;

这是 Angular 代码:

var app = angular.module('ecbc',
['ui.router',
"oc.lazyLoad",
"highcharts-ng"

]);

app.config(
function($stateProvider,$urlRouterProvider){

    $urlRouterProvider.otherwise('/');  

    $stateProvider      
    .state('input', {
        url:'/',
        templateUrl: 'templates/input.html',
        controller: 'InputController'
    })

    .state('descriptive', {
            url: '/descriptive',
            templateUrl: 'templates/descriptive.html',
            controller:'descriptiveController'
        });

});


app.controller("ecbcInputController",function($scope,$ocLazyLoad,$http){
//This is a ng-click event
$scope.grabPostData = function(){
    $http.get('/data').success(function(data){
        console.log(data);
      });
  };
  });

最佳答案

我发现解决这个问题的最好方法是使用 socket.io 的聊天功能。

这是服务器代码:

var mes='';
/* POST  */

router.post('/', function (req, res, next) {
/* Arguements from the clients request body */

/* Child process to run python scripts wrapped in Promise to
run the scripts in a synchronous form
 */

/* Script and taking in arguments*/
spawn('python', ["public/model/model.py", req.body.labRun1,req.body.operatingModel1]).progress(function (childProcess) {

    /* Response to the data that is received */
    childProcess.stdout.on('data', function (data) {
        mes = data.toString('utf8');

    });

    /* Prints Error Message */
    childProcess.stderr.on('data', function (data) {
        console.log("Child Process Error");
    });

}).then(function () {
    /* This opens up the socket listening at port 8080 */
    io.on('connection', function (socket) {
        console.log('a user connected');

        /* When client sends a message the socket server
        will respond with the model output */
        socket.on('chat message', function (msg) {
            io.emit('chat message', mes)
        });

        socket.on('disconnect', function () {
            console.log("disconnected");
        });

    });

    http.listen(8080, function () {
        console.log('listening on *:8080');
        var mes = '';
    });


}).fail(function (err) {
    console.error(err);
});

});

这是客户端代码:

$(document).ready(function () {
var socket = io('http://localhost:8080');

socket.emit('chat message', 'give me data');

socket.on('chat message', function (msg) {
    var parsedObject = JSON.parse(msg);
    $('#data').val(parsedObject);
});


})

关于angularjs - 在 Express 和 Angular Controller 中与其他路线共享子进程的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36310815/

相关文章:

javascript - angularjs 在两个模板之间共享自定义指令

javascript - 在 AngularJS 中使用带有选项选择的 ng-options

php - Angular 将数据插入 MySQL

javascript - 带有 orm2 和分离模型文件的 Node Express

javascript - 为什么使用 Mongoose 回调会导致两次保存数据?

javascript - 空文本输入框的值是多少?

javascript - Node : removing duplicate objects from array

angularjs - 带有 docker 的 Angular 应用程序 - 生产和开发

javascript - 了解 Express js 中的匿名函数

node.js - 必须使用 node.js 快速调用 res.end() 吗?