andi
andi

Reputation: 240

angular 2 npm start - how to proxy API requests to another server?

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

Upvotes: 1

Views: 2200

Answers (2)

andi
andi

Reputation: 240

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

Upvotes: 4

Olga Khylkouskaya
Olga Khylkouskaya

Reputation: 495

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.

Upvotes: 1

Related Questions