/* jshint node:true */
/* eslint-env node */
/* eslint global-require: 0, no-magic-numbers: 0 */

const proxyPath = '/proxy/bnf';

module.exports = function(app) {
  // For options, see:
  // https://github.com/nodejitsu/node-http-proxy
  const proxy = require('http-proxy').createProxyServer({});

  proxy.on('error', function(err, req) {
    console.error(err, req.url); /* eslint no-console: 0 */
  });

  app.use(proxyPath, function(req, res/* , next */) {
    // include root path in proxied request
    req.url = proxyPath + '/' + req.url;
    proxy.web(req, res, {target: 'http://data.bnf.fr/search-letter/'});
  });
};

