1

How can I proxy my AJAX calls to a different server using npm start. npm start --proxy http://localhost:8080 doesn't work

2 Answers 2

4

I did it ... One option is to add file:

bs-config.js:

var proxyMiddleware = require('http-proxy-middleware');

module.exports = {
    server: {
        middleware: {
            1: proxyMiddleware('/api', {
                target: 'http://localhost:8081/',
                changeOrigin: true
            })
        }
    }
};

Also run: npm install --save-dev http-proxy-middleware

Sign up to request clarification or add additional context in comments.

2 Comments

Where to add this file? at the root folder?
Not working for me...Please explain more...like where to put this file..I have placed in root directory of project (outside src) and then npm install --save-dev http-proxy-middleware. Then when I did npm run start, it was not working.
1

proxy option for npm is to configure npm behind the proxy. If you need to proxy localhost calls to other server try using http-proxy package: https://blog.nodejitsu.com/http-proxy-intro/

You can also add more details to your question. For example, what you are trying to achieve and your network configuration. There maybe other more elegant solution.

2 Comments

Thank for your time. The thing is that I'm new in angular 2 and I'm playing with a official tutorial. I came to http and I created simple spring based rest api. The problem is when I start my web app using [ npm start ] I can't invoke rest endpoint because of CORS. I wander if I could do a proxy without using grunt, gulp or what ever ... just using [ npm start ] with some magic parameter
In most cases when you run frontend locally and invoke some api, the api server also runs locally. If it is not your case try that: stackoverflow.com/questions/21019669/…

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.