使用node.js进行Phantomjs身份验证以访问mean.js页面

时间:2016-04-29 15:31:26

标签: javascript node.js authentication phantomjs meanjs

我需要使用phantomjs访问mean.js网站页面。问题是我不知道要在标题中包含什么来实现它。

我可以使用http.request进行身份验证并获取用户对象。但后来我需要以某种方式获取该信息并将其放在phantomjs标题中以允许访问该页面。

以下是一些代码:

'use strict';

var phantom = require('node-phantom-simple');

phantom.create({ path: require('phantomjs').path }, function (err, browser) {
  if (err) {
    console.log(err);
  }
  else {
    browser.createPage(function (err, page) {
      if (err) {
        console.log(err);
      }
      else {

//this is wrong - does not work with mean.js/passport authentication
        var authentication_data = { 'Authorization': 'Basic ' + new Buffer('<user>:<password>').toString('base64') };

        page.set('customHeaders', authentication_data, function (err) {
          if (err) {
            console.log(err);
          }
          else {

            var htmlPath = 'http://localhost:3000/path-to-my-web-page';

            var complete = false;

            page.onConsoleMessage = function (msg) {
              console.log('Console: %s', msg);
              if (msg === 'the page is loaded') complete = true;
            };

            return page.open(htmlPath, function (err, status) {
              console.log('opened page ', status);
              if (err) {
                console.log('page.open', err);
              }
              else {
                console.log('opened ' + htmlPath);
//stuff happens here after page is loaded
              }
            });
          }
        });
      }
    });
  }
});

我错过了可以访问该页面的文章。

感谢您的帮助!

0 个答案:

没有答案