所以我正在使用Node.js,Express.js和Angular.js。我想做的是获取一个子进程的结果,将其传递给路由,然后让Angular执行get请求以获取该数据。
我环顾四周,但是我看到的大多数答案都是关于数据库连接和请求的。
我要POST的路由只是普通的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);
});
})
关于angularjs - 与Express和Angular Controller中的其他路由共享子进程的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36310815/