我正在使用 React 构建一个简单的项目,我希望它连接到数据库(mongodb Atlas)。但是,我只能使用 Node 连接到我的数据库。是否可以在同一个端口 3000 上运行 Node 和 React?
我还将 Express 与 Node 一起使用,因为我发现在使用相同的 navBar 和页脚创建新页面时使用 ejs 文件非常方便。
是否可以在一个端口上同时运行这 3 个设备?
您可以通过将以下代理字段添加到 React 项目中的 package.json 来代理您的 api
"proxy": "http://localhost:4000"
Run Code Online (Sandbox Code Playgroud)
现在,在前端应用程序中,您可以使用应用程序端口进行 fetch API 调用,例如
fetch('/api/user') // this calls -> http://localhost:4000/api/user
Run Code Online (Sandbox Code Playgroud)
在这里阅读更多内容::在开发中代理 API 请求
| 归档时间: |
|
| 查看次数: |
19022 次 |
| 最近记录: |