如何基于 stdout 和 stderr 验证来自 nodejs 的 angular post 调用

how to authenticate angular post call from nodejs based on stdout and stderr

本文关键字:nodejs angular post 调用 stdout 何基于 stderr 验证      更新时间:2023-09-26

我正在使用角度全栈生成器来创建Web应用程序。 前端使用angularjs设计,服务器端使用nodejs设计。我编写了一个 python 函数来了解主机是否是 linux 主机,该函数通过 nodejs 调用,并在有效的 linux 主机的情况下写入文件"这是一个有效的主机"。如果主机是 linux 主机,我想重定向到不同的页面。我的角度代码:

$scope.sendlogin = function () { 
            console.log("inside click");
            var posting = $http({
                method: 'POST',
                url: '/login',
                data: $scope.data,
                processData: false
            })
            posting.success(function (response) {
                $location.path("/vprobe");
            });
             }

我的节点代码:

app.post('/login', function (req, res) {
/* Handling the AngularJS post request and storing in a file*/
var fs = require("fs");
console.log(req.body);
var jsonfile = require('jsonfile')
 var file = 'login.json'
 var obj = req.body;
 jsonfile.writeFile(file, obj, function (err) {
 console.error(err)
 })
// invoking python child process 
var exec = require('child_process').exec;
var arg = 'python  checkvalidity.py'
exec(arg, function(error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
console.log('exec error: ' + error);
res.send(stdout);
 });
 });

但是上面的代码无法正常工作。对于标准输出和标准,它都重定向到页面。标准输出读取一个文件,该文件的内容为"这是一个有效的主机"。如何仅在标准输出的情况下重定向,并在发生错误时显示错误。

也许在你的nodeJs代码中使用res.sendStatus(500) if (!stdout),并在你的角度控制器中使用.error(function(response)捕获它