1

I am using http-proxy-middleware to proxy hosts in my React project.

Below is the code from setupProxy.js file:

const {createProxyMiddleware} = require('http-proxy-middleware');

module.exports = function(app) {
  app.use('/api', createProxyMiddleware({ target: 'http://localhost:8080', changeOrigin: true, }));
  app.use('/api/user/logout', createProxyMiddleware({ target: 'http://localhost:9091', changeOrigin: true, }))
};

As you can see I added two proxy codes. For the logout, I want to use a different host. /api is working fine, but the /api/user/logout is not working.

Thanks in advance.

1 Answer 1

1

Because when the request goes to '/api/user/logout'. app.use('/api') finds '/api' in the url and it fits the condition of the first app.use

You need to use regExp

app.use(/\/api\/?$/, createProxyMiddleware())
Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.