如何使用 htaccess 的替代方法密码保护 nodejs 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26357724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to password protect nodejs application using alternative to htaccess
提问by dipak1296
I have a site hosted in aws with www.domain.com with 2 sub domains test.domain.com and stage.domain.com. I am using MEAN for development, now what i want is to password protected my sub domains test.domain.com and stage.domain.com but in a similar fashion as we do in php using htaccess.
我在 aws 中托管了一个站点,其中包含 www.domain.com 和 2 个子域 test.domain.com 和 stage.domain.com。我正在使用 MEAN 进行开发,现在我想要的是密码保护我的子域 test.domain.com 和 stage.domain.com,但方式与我们在 php 中使用 htaccess 所做的类似。
Now i personally like this htaccess approach because in this approach your code for the application will be same, authentication will be added by htaccess. Any idea how to do that in nodejs without altering application code, so that i can remove that in future if i want.
现在我个人喜欢这种 htaccess 方法,因为在这种方法中,您的应用程序代码将相同,身份验证将由 htaccess 添加。知道如何在不更改应用程序代码的情况下在 nodejs 中执行此操作,以便我将来可以根据需要将其删除。
P.S. : Only hints are welcomed.
PS:只欢迎提示。
回答by xShirase
.htaccess does not alter the code because it's used by the Apache webserver.
.htaccess 不会改变代码,因为它被 Apache 网络服务器使用。
Consider that Node.js IS your webserver, so that's where the magic happens (technically, it takes less code to add basicAuth to node than to add htaccess to apache + write the .htacess file).
考虑到 Node.js 是你的网络服务器,所以这就是魔法发生的地方(从技术上讲,将 basicAuth 添加到节点所需的代码比将 htaccess 添加到 apache + 编写 .htacess 文件所需的代码更少)。
Something, somewhere has to know that authentication is to be done :).
某些地方必须知道要进行身份验证:)。
Here's the link to an easy way to do that in node :
这是在 node 中执行此操作的简单方法的链接:

