rest-api-proxy

1.0.1 • Public • Published

Rest API Proxy

This package allows you to proxy a rest API. For use in API routers to create routes that proxy to the given API.

Installation

yarn add rest-api-proxy 

Usage

import express from 'express';
import restApiProxy from 'rest-api-proxy';

const app = express();

app.all('/proxy/twitter/:path*', async (req, res) => {
  const proxy = restApiProxy({
    host: 'https://api.twitter.com/v1',
    headers: {
      'X-Auth-Token': 'XXXXXXX',
    },
  });
  
  const requestData = req.body || undefined;
  
  const response = await proxy(req.method, req.param.path, {
    data: requestData,
    query: req.query,
    headers: req.headers,
  });
  
  return res.status(response.statusCode).set(response.headers).send(response.body);
});

app.listen(4000, () => {
  console.log('Server running...');
});

/rest-api-proxy/

    Package Sidebar

    Install

    npm i rest-api-proxy

    Weekly Downloads

    4

    Version

    1.0.1

    License

    MIT

    Unpacked Size

    15.7 kB

    Total Files

    7

    Last publish

    Collaborators

    • vinlock