我有一个目前在openresty / nginx下托管的聚合物网站。我正在尝试将其迁移为使用nodejs express,以便使用passportjs进行身份验证(SAML)
我已经配置了一个http-express-proxy来处理我的api调用(/ api / webapi / *)。该代理将承载令牌添加到请求中以进行身份验证。该API是ASP.net restfull Web服务。
除我的vaadin上载请求外,大多数呼叫均有效。我收到“ MIME分段流的意外结尾”错误。
代理配置
const url = require('url');
const exproxy = require('express-http-proxy');
// Adds user authorization token from passport to request
var addAuthTokenMiddleware = function (req, res, next) {
if (req.session && req.isAuthenticated()) {
req.headers['Authorization'] = 'Bearer ' + req.user.token;
next();
} else {
req.abort();
}
};
function isLoggedIn(req, res, next) {
// if user is authenticated in the session, carry on
if (req.session && req.isAuthenticated())
return next();
// if they aren't redirect them to the home page
res.redirect('/login');
};
// New hostname+path as specified by question:
const apiProxy = exproxy('http://127.0.0.1', {
forwardPath: req => url.parse(req.originalUrl).path.replace("/api/webapi/",'/api/DataAccess.svc/api/v1/'),
parseReqBody: false,
preserveHostHdr: true
});
module.exports = function(app, passport) {
app.use ('/api/webapi/*', isLoggedIn, addAuthTokenMiddleware, apiProxy);
console.log(' Proxy Loaded');
}
任何帮助将不胜感激
谢谢 迈克
答案 0 :(得分:1)
我改用http-proxy-middleware来缓解问题。
var hpmproxy = require('http-proxy-middleware');
function restream(proxyReq, req) {
if (!isEmpty(req.body)) {
var bodyData = JSON.stringify(req.body);
proxyReq.setHeader('Content-Type', 'application/json');
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
proxyReq.write(bodyData);
}
};
var options = {
target: config.webApiHost,
changeOrigin: true, // needed for virtual hosted sites
pathRewrite: {
'^/api/webapi': config.webApiPath
},
secure: !config.selfSigned,
onProxyReq: restream
};
var hpmApiProxy = hpmproxy(options);
app.use('/api/webapi/', isLoggedIn, addAuthTokenMiddleware, hpmApiProxy);