为什么http-auth不能与express.static中间件一起使用? [英] Why http-auth does not work with express.static middleware?

查看:91
本文介绍了为什么http-auth不能与express.static中间件一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 http-auth 模块设置简单的身份验证. /p>

我已经创建了此设置,但无法正常工作.出现提示用户/密码的对话框,但是如果我关闭该对话框,则该应用程序继续运行,也就是说,身份验证似乎无法正常工作.

似乎不起作用,因为我试图将其设置为在静态文件夹www中工作.

我的app.js基于这一个.

这是我的设置:

'use strict';

var express = require('express');
var app = express();
var auth = require('http-auth');

app.use(express.static('www'));

var basic = auth.basic({
  realm: 'SUPER SECRET STUFF'
}, function(username, password, callback) {
  callback(username == 'username' && password == 'password');
});

app.use("/", auth.connect(basic));

app.set('port', (process.env.PORT || 4000));
app.listen(app.get('port'), function() {
  console.log('Node app is running on port', app.get('port'));
});

解决方案

如果您浏览http://localhost:4000/,您的示例效果很好,在单击取消"后,您会看到401消息,而不是可以添加到/路由的内容(现在您可以没有路由处理程序.

要使其适用于静态文件,您还需要启用静态文件的身份验证,类似的操作会做到这一点:

'use strict';

var express = require('express');
var app = express();
var auth = require('http-auth');

var basic = auth.basic({
    realm: 'SUPER SECRET STUFF'
}, function(username, password, callback) {
    callback(username == 'username' && password == 'password');
});

app.use(auth.connect(basic));
app.use(express.static('www'));

app.set('port', (process.env.PORT || 4000));
app.listen(app.get('port'), function() {
    console.log('Node app is running on port', app.get('port'));
});

您可能会注意到,身份验证中间件auth.connect应该在静态文件中间件express.static之前声明,并且没有路由前缀,因为您的静态文件中间件没有路由前缀.

I'm trying to setup a simple auth using http-auth module.

I've created this setup but it's not working properly. The dialog prompting the user/pass show up but if I close the dialog the app continues to work, that is, the auth don't seem to be working.

Seem that's not working because I've tried to setup this http-auth to work in my static folder www.

My app.js is based on this one.

Here is my setup:

'use strict';

var express = require('express');
var app = express();
var auth = require('http-auth');

app.use(express.static('www'));

var basic = auth.basic({
  realm: 'SUPER SECRET STUFF'
}, function(username, password, callback) {
  callback(username == 'username' && password == 'password');
});

app.use("/", auth.connect(basic));

app.set('port', (process.env.PORT || 4000));
app.listen(app.get('port'), function() {
  console.log('Node app is running on port', app.get('port'));
});

解决方案

Your example works perfectly if you browse http://localhost:4000/, after hitting cancel you see 401 message instead of content that you could add to / route (now you don't have route handler for it).

To make it work for static files you just need to enable authentication for static files also, something like this will do it:

'use strict';

var express = require('express');
var app = express();
var auth = require('http-auth');

var basic = auth.basic({
    realm: 'SUPER SECRET STUFF'
}, function(username, password, callback) {
    callback(username == 'username' && password == 'password');
});

app.use(auth.connect(basic));
app.use(express.static('www'));

app.set('port', (process.env.PORT || 4000));
app.listen(app.get('port'), function() {
    console.log('Node app is running on port', app.get('port'));
});

As you may notice authentication middleware auth.connect should be declared before static file middleware express.static and without route prefix as your static file middleware does not have route prefix.

这篇关于为什么http-auth不能与express.static中间件一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆