页面没有返回结果

时间:2014-11-30 12:05:23

标签: javascript node.js callback

我有节点js +表达4个问题。我试图从login.js调用index.js中的函数并打印出答案,但它甚至没有给我一个错误原因。 控制台日志在控制台中提供了我想要的内容,但它不会在页面上呈现。

index.js:

var express = require('express');
var router = express.Router();
var login = require('./login.js');

/* GET home page. */
router.get('/', function(req, res) {
res.render('index', { title: 'Express' });
});

/* Check login status */
router.get('/login', function(req, res) {
var response = login.logintest();
res.send(response);

});

module.exports = router;

login.js:

/* LOAD all requires needed here */
var request = require('request');
    request = request.defaults({jar: true});
var cheerio = require('cheerio');
/*
 * GET logincheck check if user needs to login
 */

exports.logintest = logintest;

function logintest(req, res)
      {
      request.get('http://internalbusinessdomain.com/', getStatus);
      };
function getStatus(err, mess, body)
      {
        if (!err && mess.statusCode == 200)
        {
            // console.log(body) ;
          var $Cheers=cheerio.load(body);
          // console.log($Cheers('li').text());
          if ($Cheers('li').text().substring(0,8) == "Inloggen"){
            console.log("htli")
            return "hastologin";

          }
          else {console.log("verbonden");
            return "connected";
          };
        }
        else
        { console.log("niet verbonden met tln");
          return "notconnected";
        };
      };

1 个答案:

答案 0 :(得分:1)

您必须使用回调而不是返回您的代码,如下所示

var express = require('express');
var router = express.Router();
var login = require('./login.js');

/* GET home page. */
router.get('/', function(req, res) {
res.render('index', { title: 'Express' });
});

/* Check login status */
router.get('/login', login.logintest);

module.exports = router;

login.js

var request = require('request');
    request = request.defaults({jar: true});
var cheerio = require('cheerio');
/*
 * GET logincheck check if user needs to login
 */

exports.logintest = logintest;

function logintest(req, res){
    request.get('http://internalbusinessdomain.com/',function (err, mess, body){
        if (!err && mess.statusCode == 200){
            // console.log(body) ;
            var $Cheers=cheerio.load(body);
            // console.log($Cheers('li').text());
            if ($Cheers('li').text().substring(0,8) == "Inloggen"){
                console.log("htli")
                res.send("hastologin");
            }else {
                console.log("verbonden");
                res.send("connected");
            };
        }else{
            console.log("niet verbonden met tln");
            res.send("notconnected");
        };  
    });
}