Sign in
Log inSign up

Which approach is beneficial to call apis from server http-proxy or superagent ? and why?

Ghanshyam K Dobariya's photo
Ghanshyam K Dobariya
·Dec 1, 2017

I am calling API. Below are 2 code snipets.

I have haproxy installed in my linux machine to ..

// Superagent approch
import superagent from 'superagent';
app.get('/api/getProducts', (req, res) => {
  const endPoint = '127.0.0.1:8500/getProducts';
  superagent.get(endPoint)
  .timeout(1000)
  .end((error, response) => {
    const { body, text } = response || {};
    if (response) {
      ret = text || body;
    } else if (error) {
      ret = error;
    }
    return res.send(ret);
  });
});


// http proxy approch

import { createProxyServer } from 'http-proxy';
const proxy = createProxyServer({
  target: '127.0.0.1:8500',
  proxyTimeout: 1000,
});
app.get('/api/getProducts', (req, res) => {
  proxy.web(req, res);
});
proxy.on('error', (err, req, res) => {
  const json = { error: 'proxy_error', message: err.message };
  res.end(JSON.stringify(json));
});
Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct