Angularjs 共享子进程&x27;s与快速和角度控制器中的其他路线的结果

Angularjs 共享子进程&x27;s与快速和角度控制器中的其他路线的结果,angularjs,node.js,express,child-process,Angularjs,Node.js,Express,Child Process,因此,我正在使用Node.js、Express.js和Angular.js。我要做的是获取子进程的结果,将其传递给路由,然后让Angular执行get请求以获取该数据 我环顾四周,但我看到的大多数答案都是关于数据库连接和请求的 我要发布的路线只是一条普通的Express.js路线 这是我的密码: Index.js: var express = require('express'); var router = express.Router(); var spawn = require('child

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

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

我要发布的路线只是一条普通的Express.js路线

这是我的密码: Index.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;
以下是角度代码:

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);
});


})

我发现解决这个问题的最好方法是使用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);
});


})