我正在尝试使用proxy-middleware转发至https://npmcdn.com/当我指定最终路径时,它会起作用,但是当我指定速记网址时,它们会被重定向到本地服务器的 /
,因此返回 404。
app.use('/cdn', proxy('https://npmcdn.com/'));
然后尝试访问本地服务器...
$.get('/cdn/lodash@4.9.0'); // works as expected
$.get('/cdn/lodash'); // is redirected to `/lodash@4.9.0` so gets 404
如何使用代理服务器处理重定向,以便将其重定向到正确的远程 URL?
更新:
@hassansin,感谢您的指点。看起来我可以通过添加一个条件来处理没有域的绝对路径来解决我的问题,你认为它会破坏其他任何东西吗?
if (((statusCode > 300 && statusCode < 304) || statusCode === 201) && location) {
// absolute path with domain
if(location.indexOf(options.href) > -1) {
headers.location = location.replace(options.href, slashJoin('/', slashJoin((options.route || ''), '')));
// absolute path without domain
} else if(location[0] === '/') {
headers.location = options.href.replace(/\/$/,'') + headers.location;
}
}
更新 2:
@hassansin,这个怎么样?
// absolute path without domain
} else if(location[0] === '/' && req.originalUrl.slice(req.url.length*-1) === req.url) {
headers.location = req.originalUrl.slice(0, req.url.length*-1) + headers.location;
}
最佳答案
https://npmcdn.com/lodash
重定向到 https://npmcdn.com/lodash@4.9.0
,带有 302 状态和 location:/lodash@4.9.0
header 。 proxy-middleware
似乎无法正确处理相对的 302 重定向。 It only handles absolute redirects .
关于node.js - Node 表达代理中间件句柄重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36534971/