4 proxy http http-proxy node.js express
我是IP限制纯客户端CORs演示应用程序,它与经过身份验证的第三方API交互.我有一个"中间件"服务器运行,我用它来代理从CORs应用程序到第三方API的请求,但我无法将基本身份验证凭据注入这些代理请求.
isAllowed = (req, res, next) -> # Do IP check here.
base64Encode = (unencoded) -> new Buffer(unencoded or '').toString 'base64'
app.all "/demoproxy/*", isAllowed, (req, res) ->
req.url = "/" + req.url.split("/").slice(2).join("/")
userPass = base64Encode "#{process.env.DEMO_USERNAME}:#{process.env.DEMO_PASSWORD}"
# This doesn't work.
# res.setHeader 'Authorization', "Basic #{userPass}"
# This doesn't work either.
###res.oldWriteHead = res.writeHead
res.writeHead = (statusCode, headers) ->
headers = { }
headers['Authorization'] = "Basic #{userPass}"
res.oldWriteHead statusCode, headers###
proxy = new httpProxy.HttpProxy
target:
host: 'remote-api.com'
port: 80
proxy.proxyRequest req, res
Run Code Online (Sandbox Code Playgroud)
这样做的正确方法是什么?
小智 11
我想你想在这种情况下在请求(req)对象上设置授权头,而不是响应(res).如果remote-api.com需要进行身份验证,那么它需要知道您发送给它的请求.也许在提出proxy.proxyRequest请求之前尝试以下方法
req.headers["authorization"] = "Basic #{userPass}"
Run Code Online (Sandbox Code Playgroud)
由于该req对象没有setHeader函数,headers属性只是一个javascript对象/ map.希望有助于......
| 归档时间: |
|
| 查看次数: |
14483 次 |
| 最近记录: |