reactjs - HapiJS + create-react-app + webpack-dev-middleware 配置
问题描述
我目前正在使用create-react-app
带有 HapiJS 作为代理来提供静态文件。
这是我当前设置的样子:
//server.js
const server = new Hapi.Server({
port: process.env.PORT || 80,
routes: {
files: {
relativeTo: Path.join(__dirname, '../build')
}
}
});
await server.register(Inert);
server.route(routes); // comes from an array of routes defined in react-routes.js
await server.start();
console.log('Server running at:', server.info.uri);
//react-routes.js
//All the routes mapped here should return an index.html file.
//React will take care of routing.
const routes = [
{ method: 'GET', path: '/home', config: fileHandler }
{ method: 'GET', path: '/account', config: fileHandler }
]
const fileHandler = {
handler: (req, res) => {
return res.file('index.html');
}
}
我nodemon
在开发中使用来运行服务器并react-scripts start
运行 react-app。现在的问题是,HapiJS 服务器只提供build
使用react-scripts build
. 我知道为了连接到 webpack 构建,我需要为 HapiJS 使用中间件。
查看webpack 文档,我发现它需要对webpack.config.js
文件的引用。但我不能随意使用eject
以webpack.config.js
根据需要访问。
所以问题是:
- 如何
webpack-dev-middleware
与 HapiJS 一起设置create-react-app
? - 如何在
webpack.config.js
不弹出的情况下访问(最好不使用任何第 3 方模块)?
注意:这不是服务器端渲染,我使用 HapiJS 作为代理,因为涉及跨域 API 调用,代理帮助我避免臭名昭著的 CORS 错误并为应用程序提供服务。
解决方案
推荐阅读
- powershell - Azure PowerShell Connect-AzureAD 和 Connect-MsolService 都带有 MFA 连接
- sql - 使用 XML PATH 在一个 XMLFILE 中的多个图像
- r - 如何从 lmer 模型中仅提取随机效应相关参数?
- php - 如何在 WooCommerce 商店页面上“缺货”10 天后隐藏缺货产品
- postgresql - Postgres.exe 崩溃并关闭所有应用程序,恢复并再次运行
- javascript - Javascript const变量在与另一个变量相乘后发生变化
- css - 带有文本溢出省略号的非常长标签的按钮的最大宽度为 700 像素,但如果视口变小应该缩小,不知道
- python - 创建 3x3 matplotlib 子图,但在某些位置缺少图
- .net - dot net 应用程序未构建。找不到错误包
- php - TCPDF:为什么多单元格的位置是从另一个元素继承的?