Express JS proxy to call web api Express JS proxy to call web api express express

Express JS proxy to call web api


You can get the URL to be proxied as a query parameter, modify it and then pass that URL to proxy, like this (use instead of app.use('/proxy', proxy('http://localhost:56660/api/values'));):

app.get('/proxy', (req, res, next) => {    const modifiedURL = modifyURL(req.query.url)    return proxy(modifiedURL)(req, res, next)})

You can call you server with an URL like this (GET method):

https://my.server.com/proxy?url=https://urltobeproxied.com

UPDATE:

I think this would work according to your needs:

app.use('/proxy', (req, res, next) => {    const requestedUrl = `${req.protocol}://${req.get('Host')}${req.url}`    const modifiedURL = modifyURL(requestedUrl)    proxy(modifiedURL)(req, res, next)})

UPDATE2:

app.use('/proxy', proxy('http://localhost:56660/api/values', {  proxyReqPathResolver: function(req) {    const requestedUrl = `${req.protocol}://${req.get('Host')}${req.url}`    const modifiedURL = modifyURL(requestedUrl)    return require('url').parse(modifiedURL).path;  }}))

UPDATE3:

An example of proxy modifying the response (extracted from the package docs);

app.use('/proxy', proxy('http://localhost:56660/api/values', {  userResDecorator: function(proxyRes, proxyResData, userReq, userRes) {    data = JSON.parse(proxyResData.toString('utf8'));    data.newProperty = 'exciting data';    return JSON.stringify(data);  }}))