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
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
2 Comments
Tarun Nagpal
Where to add this file? at the root folder?
hemu
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.
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
andi
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
Olga Khylkouskaya
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/…