ben*_*e89 3 node.js express webpack webpack-dev-server create-react-app
我正在处理一个弹出的Create React App项目,并查看webpack开发服务器上的文档,它们看起来有点裸露:https ://webpack.js.org/configuration/dev-server/#devserver-before
但我试图查看是否可以执行以下操作:
before(app){
// read cookie for user session
// send user ID in cookie to external API
// retrieve user object from API
// attach user object to response, to be _somehow_ accessed via the React app client side
}
Run Code Online (Sandbox Code Playgroud)
我知道这是伪代码,但我什么很不清楚究竟你可以在这个中间件中做,在挂钩到创建作出反应的index.html和上述客户端的应用程序的渲染方面反应应用
在文档中,它说您可以定义一个路由处理程序,如下所示:
app.get('/some/path', function(req, res) { }
但是我觉得那将不会有用,因为那样一来您将无法回到Webpack开发服务器的渲染过程中来?
您可以尝试将其添加到您的webpack配置文件中,实际上您可以将其用作模拟服务器。这是我如何配置我的并希望对我有所帮助的示例。
devServer: {
historyApiFallback: true,
noInfo: true,
overlay: true,
before:(app) => {
app.post('/user/login', function(req, res, next) {
res.json({success: true})
});
}
},
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3180 次 |
| 最近记录: |