基于stdout和std的nodejs角后呼叫认证

2024-04-20 06:04:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用angular fullstack generator创建web应用程序。前端使用angularjs设计,服务器端使用nodejs。我已经编写了一个python函数来知道一个主机是否是linux主机,它通过nodejs调用,并在linux主机有效的情况下写入一个文件“This is a valid host”。如果主机是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);
 });

 });

但是上面的代码工作不正常。对于stdout和stderr,它将重定向到页面。stdout读取一个包含“This is a valid host”内容的文件。如何仅在标准输出时重定向,并在错误时显示错误。你知道吗


Tags: 代码logvarlinuxstderrstdoutloginfunction