Skip to content

Instantly share code, notes, and snippets.

@popuguytheparrot
Created October 1, 2019 09:56
Show Gist options
  • Save popuguytheparrot/f7f8c6a7cd454be72f52ddab66bc8370 to your computer and use it in GitHub Desktop.
Save popuguytheparrot/f7f8c6a7cd454be72f52ddab66bc8370 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(data => console.log).catch(e => console.error(e.message))
import ky from 'ky';
const isProd = process.env.NODE_ENV === 'production';
const prefixUrl = isProd ? '' : 'http://localhost:3000';
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