【发布时间】:2019-05-23 10:58:12
【问题描述】:
我是 Express 的初学者,我正在使用中间件实现一个相当奇怪的功能。在这里,我调用一个由其中间件获取的 URL,然后在 next() 上调用另一个中间件。现在在第二个中间件的next() 中,我需要加载组件,但问题是,在第一个中间件的next() 之后,URL 没有改变。
代码:
Express 应用程序:路由器:
app.use('/common/global/login', mainHandler);
app.use('/common/*', subhandler, SuccessComponent);
中间件:
export function mainHandler(req, res, next) {
const global-url= "someURL"
if (global-url) {
return fetch(global-url)
.then((response) => response.json())
.then((response) => {
if (response.data) {
next();
} else {
throw Error(response.statusText);
}
})
.catch((error) => {
res.redirect('/session-expired');
next(error);
});
}
res.redirect('/session-expired');
}
export function subhandler (req, res, next) {
const other_url= "someOtherURL"
return fetch(other_url)
.then((response) => response.json())
.then((response) => {
if (response.data) {
// here it not loading the SUCCESSCOMPONENT as the URL still remains /common/global/login
return next();
}
throw Error(response.statusText);
})
.catch((error) => {
next(error);
res.redirect('/session-expired');
});
}
res.redirect('/session-expired');
}
【问题讨论】:
标签: node.js reactjs express middleware