【发布时间】:2017-01-18 20:49:29
【问题描述】:
我正在探索 node 并使用 redux 表达,我想在页面呈现后设置一个 cookie,并想使用更新的状态来设置一个我收到此错误的 cookie。
请帮助我获得以下答案? 1)让我知道我写的语法是否正确,如果不正确,那么应该怎么做? 2) 成功渲染 ejs 文件后如何设置 cookie 响应?
router.get('/dashboard',isLoggedIn,(req, res) => {
store.dispatch(initialize(reduxOauthConfig))
.then(() => match({ routes: dashroutes, location: req.url }, (error, redirectLocation, renderProps) => {
if (redirectLocation) {
res.redirect(301, redirectLocation.pathname + redirectLocation.search);
} else if (error) {
res.status(500).send(error.message);
} else if (!renderProps) {
res.status(404).send('Not found');
} else {
loadOnServer({ ...renderProps, store })
.then(() => {
const componentHTML = ReactDOMServer.renderToString(
<Provider store={store}>
<ReduxAsyncConnect {...renderProps}/>
</Provider>
);
const initialState = store.getState();
res.render('dashboard.ejs', {
markup: componentHTML,
intialState:initialState
});
})
.then(html => {
// !!! IMPORTANT TO PERSIST SESSION IF JavaScript failed to load / initialize
res.cookie('authHeaders', JSON.stringify(getHeaders(store.getState())), { maxAge: now() * 0.001 + 14 * 24 * 3600 });
res.end(html);
})
.catch(err => {
console.log(err.stack);
res.end(err.message);
});
}
}));
});
【问题讨论】:
-
@JohnWeisz - 我不会称这是一个重复。这里的问题是为什么此特定代码会生成该错误消息以及此代码的违规部分是什么。其他答案没有解决有关此特定代码有什么问题的任何问题。
-
@jfriend00 -- 虽然我同意你的观点,但我也相信在这种情况下这归结为品味和个人喜好问题,考虑到这是服务器端编程的经典问题,其中了解问题背后的一般原则本身就是答案:在标题之前不写入内容,或使用输出缓冲。
标签: node.js reactjs express redux