How can I return a valid JSON response with React-Router and an Express backend?

风流意气都作罢 提交于 2020-05-24 04:55:44

问题


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.

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.


回答1:


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

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


来源:https://stackoverflow.com/questions/61160329/how-can-i-return-a-valid-json-response-with-react-router-and-an-express-backend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!