简体   繁体   English

使用HTTP-AUTH的node.js Express

[英]node.js Express with HTTP-AUTH

I have problem with creating node.js application based on express with http-auth module. 我在基于带有http-auth模块的express创建node.js应用程序时遇到问题。

It's a possible create Middleware for this http-auth library I have this code: 这可能是为此http-auth库创建中间件的代码,我有以下代码:

    //Configure Middlewares
logger.configure(function() {

  //All output has content type json
  logger.use(function(req, res, next) {
    res.contentType('application/json');
    next();
  });

  //Create digest auth middleware
  logger.use(function(req, res, next){
    digest.apply();
    next();
  });
});

After applying this, when I connect to site I get this error: 应用此方法后,当我连接到站点时出现此错误:

TypeError: Cannot read property 'headers' of undefined

Is there any solution for this problem or use another method? 是否有解决此问题的方法或使用其他方法?

I need digest auth for the whole application. 我需要整个应用程序的摘要身份验证。

I think apply() is expecting the request object (hence it can't read the headers); 我认为apply()期望请求对象(因此无法读取标头);

try this syntax: 试试这个语法:

digest.apply(req, res, function(username) {

});   

You should just use it with correct pattern : 您应该以正确的模式使用它:

// Authentication module.
var auth = require('http-auth');
var digest = auth.digest({
  realm: "Simon Area.",
  file: __dirname + "/../data/users.htdigest"
});

// Configure Middlewares
logger.configure(function() {
  // Create digest auth middleware
  logger.use(auth.connect(digest));

  // All output has content type json.
  logger.use(function(req, res, next) {
    res.contentType('application/json');
    next();
  });    
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM