如何在同一端口上运行 Json 服务器和 ReactJS
我正在尝试使用 JSON 服务器作为我的后端 API 来运行 React 应用程序。 但是在不同端口上运行时出现 CORS 错误
我需要一些解决方案,以便我可以在同一端口上运行 提前致谢!
I am trying to run React application using JSON server- as my backend API.
But getting CORS error while running on different port
I need some solution so that i can run on same port
Thanks in Advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能两次打开同一端口。
You have basically two options
Proxy
You can integrate proxy directly using nodejs - CRA supports this option:
实施CORS CORS
ILLACH CORS ILLACHEN CORS HEARBER并不那么困难看起来像。
请参阅JSON服务器的文档: https://www.npmjs.coms.coms.coms.coms.coms.com/package/json/json -Server
如果您直接通过命令行使用JSON服务器,也有一个CLI选项。默认情况下,启用了CORS。因此,似乎您需要
jsonserver.defaults()
来创建中间件。You can't open the same port twice.
You have basically two options
Proxy
You can integrate proxy directly using nodejs - CRA supports this option: https://create-react-app.dev/docs/proxying-api-requests-in-development/
Implement CORS
Implement CORS headers is not as hard as it might look like.
Please see the documentation of the json server: https://www.npmjs.com/package/json-server
There is also a CLI option if you use json-server directly via command line. By default, CORS are enabled. Therefore it seems that you need the
jsonServer.defaults()
to create your middlewares.