如何使用 React-Router 和 Express 后端返回有效的 JSON 响应? [英] How can I return a valid JSON response with React-Router and an Express backend?

查看:30
本文介绍了如何使用 React-Router 和 Express 后端返回有效的 JSON 响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于这个 教程.如何单独呈现 JSON(无 HTML)而不必为 index.js 中指定的路由指定端口号?在我自己的设置中,我可以通过访问 https://socialmaya.com/u/real 来查看 JSON但除非我访问 http://socialmaya.com:5000/actors/,否则无法获得实际的 JSON 响应真实的,这就是 React 所调用的.

I have a setup similar to the one in this tutorial. How can I render JSON alone (no HTML) without having to specify the port number for the route specified in index.js? Within my own setup, I can view JSON by visiting https://socialmaya.com/u/real but can't get an actual JSON response unless I visit http://socialmaya.com:5000/actors/real, which is what's being called by React.

这是我的 代码,它尝试为使用构建的社交网络实现 ActivityPub APIMERN 堆栈.我的 Nginx 配置可以在这里找到.

Here is my code which is an attempt to implement an ActivityPub API for a social network built using the MERN stack. My Nginx configuration can be found here.

推荐答案

在 Nginx 中将 URL 传递给上游之前重写 URL 解决了我的问题:

Rewriting the URL before passing it upstream within Nginx solved my problem:

location / {
    rewrite ^/u/(.*)$ /actors/$1 last;
    proxy_pass http://localhost:5000;
}

这篇关于如何使用 React-Router 和 Express 后端返回有效的 JSON 响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆