ato*_*era 2 node.js reactjs webpack babeljs create-react-app
我想在我的新 Web 应用程序中使用 React 作为前端,使用 Express 作为后端。
根据指南,我必须在项目目录中使用create-react-app工具创建子项目:
my-project
|-frontend
| |-package.json
| |-components
| | |-...
| |
| |-...
|
|-backend
| |-api
| | |-...
| |-app.js
| |-...
|
|-package.json
Run Code Online (Sandbox Code Playgroud)
在开发过程中,我应该在不同的端口上启动两个服务器,分别用于后端和前端,并且对后端的代理请求抛出React服务器。
这个计划对我来说看起来很丑陋。如何配置后端服务器以提供与react-scripts相同的功能,以避免使用单独的服务器
必须分离文件夹(用于后端和前端)将为您的项目提供更清晰和可扩展的结构。
在后端创建一个package.json文件并在脚本部分中添加以下内容:
"scripts": {
"start": "node index.js" // the command you use to run your project
}
Run Code Online (Sandbox Code Playgroud)
在同一个脚本中运行两个项目的方式:
安装npm-run-all包
$ npm install npm-run-all --save-dev
Run Code Online (Sandbox Code Playgroud)
然后在你的文件my-project/package.json中你应该有这样的内容:
"scripts": {
"start:server": "cd backend && npm start",
"start:client": "cd frontend && npm start",
"start": "npm-run-all --parallel start:client start:server"
}
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你!
归档时间: |
|
查看次数: |
2295 次 |
最近记录: |