gpt4 book ai didi

node.js - 宇博/ express : disable basic auth in a specific route

转载 作者:太空宇宙 更新时间:2023-11-03 22:37:27 25 4
gpt4 key购买 nike

我正在使用 Hubot,并且定义了环境变量 EXPRESS_USER 和 EXPRESS_PASSWORD 来启用基本身份验证。 Hubot 使用express,基本上就是

setupExpress: ->
user = process.env.EXPRESS_USER
pass = process.env.EXPRESS_PASSWORD
stat = process.env.EXPRESS_STATIC

express = require 'express'

app = express()

app.use (req, res, next) =>
res.setHeader "X-Powered-By", "hubot/#{@name}"
next()

app.use express.basicAuth user, pass if user and pass
app.use express.query()
app.use express.bodyParser()
app.use express.static stat if stat`

我想在不需要基本身份验证的脚本中公开 HTTP 命令。但是我无法更改 Hubot 中表示正在初始化的代码

robot.router.get '/some-anonymous-path', (req, res) ->
console.log 'Should be here without need to authenticate

有谁知道是否可以在expressjs中做到这一点。

提前致谢

布鲁诺

最佳答案

把 nginx 放在你的 Hubot 前面怎么样?然后,您还可以添加 SSL、仅允许访问特定路径、重写 url,甚至提供由 hubot 脚本创建的静态内容。一个简单的 nginx.conf block 示例:

upstream hubot {
server localhost:8080;
}
server {
listen 80;
satisfy any;
allow 127.0.0.1;
deny all;
location ~ ^/(public|obscured) {
allow all;
proxy_pass http://hubot;
}
location ~ ^/(static) {
auth_basic "Restricted";
auth_basic_user_file htpasswd;
root /www;
}
location ~ {
auth_basic "Restricted";
auth_basic_user_file htpasswd;
proxy_pass http://hubot;
}
}

然后在/etc/nginx/htpasswd 和你的 hubot 初始化脚本中添加一个 htpasswd 对 BIND_ADDRESS=localhost (默认绑定(bind)是 0.0.0.0),你就可以开始比赛了。

关于node.js - 宇博/ express : disable basic auth in a specific route,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24392180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com