Skip to content

Instantly share code, notes, and snippets.

@sholladay
Forked from popuguytheparrot/api.js
Last active October 1, 2019 11:16
Show Gist options
  • Save sholladay/7316505d5f7cc5a9fc596973e1cb4c7d to your computer and use it in GitHub Desktop.
Save sholladay/7316505d5f7cc5a9fc596973e1cb4c7d to your computer and use it in GitHub Desktop.
ky with proxy
import { ky } from './ky.config';
const employeesUrl = 'employees';
export async function fetchEmployees() {
return ky.get(employeesUrl).json();
}
import { fetchEmployees } from './api'
fetchEmployees().then(console.log).catch(e => console.error(e.message))
import ky from 'ky';
const isProd = process.env.NODE_ENV === 'production';
const prefixUrl = isProd ? '/api/v1/' : 'http://localhost:3000/api/v1/';
const api = ky.create({ prefixUrl });
export { api as ky };
const express = require('express');
const proxy = require('http-proxy-middleware');
const app = express();
const target = 'http://dummy.restapiexample.com';
app.use(
'/api/v1',
proxy({
target,
changeOrigin: true,
secure: false,
logLevel: 'debug'
})
);
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment