Issue
I have a Spring MVC application (jsp as a views) and i want to add react.js as a frontend. I have a couple of questions: 1. Where i should place my create-react-app folder or it doesn't matter. 2. Is there a way to run the application from maven (war) only. I mean that i know only one way to run application it is start backend from maven and start react.js from npm start. But this approach requires to use proxy or cors (or i'm wrong). I am using IntellijI IDEA and WebStorm.
Solution
- It doesn't matter
- You are right that you should run it on different servers for spring and react.
- You can proxy your api request to the backend to avoid CORS issue.
Copy this to your src/setupProxy.js
in your create-react-app. More info here
const proxy = require("http-proxy-middleware");
module.exports = app => {
app.use(
proxy("/api", {
target: "http://jsonplaceholder.typicode.com",
pathRewrite: { "^/api": "" },
changeOrigin: true
})
);
};
Then you can proxy request to your backend using /api
prefix. Example i can do something like fetch('/api/users')
in my frontend react app
Answered By - Jasper Bernales