我正在运行SSR React node.js服务器,我需要一些有关如何解决问题的建议。我的node.js服务器崩溃/重新启动,原因是未定义窗口等错误,而不仅仅是注销并打印出500条错误消息。
这是我的信息流:
Collection {#499 ▼
#items: array:22 [▼
0 => tag {#524 ▶} //parent1
1 => tag {#525 ▶} //a
2 => tag {#526 ▶} //b
3 => tag {#527 ▶} //d
4 => tag {#528 ▶} //parent2
5 => tag {#529 ▶} //c
6 => tag {#530 ▶} //f
7 => tag {#530 ▶} //parent3
8 => tag {#530 ▶} //e
]
}
我以为我的钓钩会出现那些错误,但显然不是,有人可以帮助我指出正确的方向吗?
更新:1 我目前正在尝试:
try {
.......
.......
.......
const markupStream = renderToNodeStream(
<Provider store={store}>
<ConnectedRouter history={history} location={context}>
<App/>
</ConnectedRouter>
</Provider>
);
if (context.url) {
return res.redirect(301, context.url)
}
return fs.createReadStream(htmlFilePath)
.pipe(htmlReplace('#root', markupStream))
.pipe(replaceStream('__SERVER_DATA__', serialize(store.getState())))
.pipe(res);
} catch (err) {
const errMarkup = renderToNodeStream(
<Provider store={store}>
<ConnectedRouter history={history} location={context}>
<Error500 error={err}/>
</ConnectedRouter>
</Provider>
);
logger.log({
level: 'error',
message: `Rendering ${req.originalUrl} fallback to Error render method`,
...{
errorMessage: err.message,
stack: err.stack
}
});
return fs.createReadStream(htmlFilePath)
.pipe(htmlReplace('#root', errMarkup))
.pipe(res.status(500));
} finally {
logger.info(`Request finished ${req.originalUrl} :: ${res.statusCode}`)
end({ route: path, componentName: componentNames[0], code: res.statusCode })
logger.profile(profileMsg);
}
答案 0 :(得分:1)
您需要先创建流,然后再将其返回,以便使用其错误回调函数:
var readStream = fs.createReadStream(filename);
readStream.on('error', function(err) {
res.end(err);
});
return readStream