gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 02:14:37 25 4
gpt4 key购买 nike

所以我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com