Skip to content

Instantly share code, notes, and snippets.

@blia
Last active February 20, 2019 19:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blia/c15300257b31655e1d7f9aa00614e882 to your computer and use it in GitHub Desktop.
Save blia/c15300257b31655e1d7f9aa00614e882 to your computer and use it in GitHub Desktop.

index.js

const express = require('express');
// $ npm i glob
const globSync   = require('glob').sync;

let app = express();

let proxies = globSync('./proxies/**/*.js', { cwd: __dirname }).map(require);

proxies.forEach(route => route(app));

//...

app.listen(PORT);

./proxies/mk.js

const proxyPath = '/api';
// $ npm i http-proxy
const proxy = require('http-proxy').createProxyServer({});

module.exports = app => {
  proxy.on('error', (err, req) => console.error(err, req.url));

  app.use(proxyPath, (req, res) => {
    proxy.web(req, res, { target: 'https://hub.modnakasta.ua/' });
  });
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment