Simple Node.js Proxy

C

codeguru

Member
Freecoin
295
When I wanted to refresh my React.js skills, I quickly moved to create a dashboard of cryptocurrencies, their prices, and and other aspects of digital value. Getting rolling with React.js is a breeze — create-react-app {name} and you’re off and running. Getting the API working isn’t quick, especially if they don’t accept cross-origin requests.

I set out to find the easiest possible Node.js proxy and I think I found it: [URL='https://github.com/chimurai/http-proxy-middleware']http-proxy-middleware[/URL]; check out how easy it was to use:


// ... after `npm install express http-proxy-middleware`

const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');

const app = express();
app.use('/coins/markets', createProxyMiddleware({
target: 'https://api.coingecko.com/api/v3/co..._cap_desc&per_page=100&page=1&sparkline=false',
headers: {
accept: "application/json",
method: "GET",
},
changeOrigin: true
}));
app.listen(3001);


After node server.js is executed, I can hit http://localhost:3001/coins/markets from my React app and receive quotes from CoinGecko’s API. Perfect!

I’m so grateful for projects like [URL='https://github.com/chimurai/http-proxy-middleware']http-proxy-middleware[/URL] ; they allow us to easily move past development issues and help us move forward!

The post Simple Node.js Proxy appeared first on David Walsh Blog.



Continue reading...
 

Richest Freecoded User

Most Freecoin

freecoded
freecoded
2,465 Freecoin
Davy200
Davy200
590 Freecoin
nathan69
nathan69
424 Freecoin
Laureine
Laureine
415 Freecoin
C
codeguru
295 Freecoin
Tekera
Tekera
263 Freecoin
R
ruthailand
221 Freecoin
A
Akubay
170 Freecoin
smitha
smitha
104 Freecoin
G
Gabby
93 Freecoin
Top