krishan singh
krishan singh

Reputation: 1

How to implement this scenario: calling api on serverA to automatically go to serverB and run the request and return the response to serverA

I am using NodeJS along with ExpressJS for my application. I have written some API as well. My local server is running on port 3000.

I will be calling my API, say on some particular location like: http://firstdomain.com/api/[apiname] so I should have to access only this.

When I call this http://firstdomain.com/api/[apiname] it should automatically go to http://seconddomain.com/api/[apiname] and get the result and send back to http://firstdomain.com/api/[apiname].

So as a person I will call only http://firstdomain.com/api/[apiname] and actually it will go to http://seconddomain.com/api/[apiname] to fetch data and return to me on http://firstdomain.com/api/[apiname]. I won't be knowing that the data came from the second domain link. Hope I explained.

Thanks in advance

Upvotes: 0

Views: 37

Answers (1)

mihai
mihai

Reputation: 38563

What you need is a simple proxy server. There are several modules out there that plug directly into express as middleware.

For example http-proxy-middleware allows you to do this:

app.use('/api', proxy({target: 'http://seconddomain.com', changeOrigin: true}));

Add this line to your firstdomain server code and it will act as a proxy.

Upvotes: 0

Related Questions