2016-03-30 12 views
1

Bu yüzden Node.js, Express.js ve Angular.js ile çalışıyorum. Yapmak istediğim, bir alt işlemin sonucunu almak, bunu bir rotaya aktarmak ve sonra Angular'ın bu verileri elde etmek için bir istek isteğini gerçekleştirmesidir.Çocuk İşleminin sonuçlarını Ekspres ve Eğik Denetleyicide başka bir yolla paylaşma

Etrafa baktım, ancak gördüğüm cevapların çoğu veritabanı bağlantıları ve istekleri üzerine olmuştur.

POST'a gideceğim rota normal bir Express.js yoludur. Index.js: Bunu çözmek için en iyi yol socket.io sohbet özelliğini kullanarak yoluyla olduğu tespit

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

cevap

0

:

İşte
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; 

açısal kodudur İşte

benim kodudur . istemci kodu İşte

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

}); 

edilir:

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


}) 
İşte

sunucu kodudur
İlgili konular