Guest User

Untitled

a guest
Oct 20th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. var httpProxy = require('http-proxy');
  2.  
  3. var proxy = httpProxy.createServer({
  4. target:'http://petrpavlik.apiary-mock.com'
  5. });
  6.  
  7. proxy.listen(8005);
  8.  
  9. proxy.on('error', function (err, req, res) {
  10. res.writeHead(500, {
  11. 'Content-Type': 'text/plain'
  12. });
  13.  
  14. res.end('Something went wrong. And we are reporting a custom error message.');
  15. });
  16.  
  17. proxy.on('proxyRes', function (res) {
  18. console.log('RAW Response from the target', JSON.stringify(res.headers, true, 2));
  19. });
  20.  
  21. Petrs-MacBook-Air-2:~ petr$ curl -v 127.0.0.1:8005/notes
  22. * About to connect() to 127.0.0.1 port 8005 (#0)
  23. * Trying 127.0.0.1...
  24. * Adding handle: conn: 0x7f977280fe00
  25. * Adding handle: send: 0
  26. * Adding handle: recv: 0
  27. * Curl_addHandleToPipeline: length: 1
  28. * - Conn 0 (0x7f977280fe00) send_pipe: 1, recv_pipe: 0
  29. * Connected to 127.0.0.1 (127.0.0.1) port 8005 (#0)
  30. > GET /notes HTTP/1.1
  31. > User-Agent: curl/7.30.0
  32. > Host: 127.0.0.1:8005
  33. > Accept: */*
  34. >
  35. < HTTP/1.1 404 Not Found
  36. < cache-control: no-cache, no-store
  37. < content-type: text/html; charset=utf-8
  38. < date: Sat, 28 Jun 2014 09:40:29 GMT
  39. * Server MochiWeb/1.0 (Any of you quaids got a smint?) is not blacklisted
  40. < server: MochiWeb/1.0 (Any of you quaids got a smint?)
  41. < content-length: 2960
  42. < connection: Close
  43. <
  44.  
  45. var apiProxy = httpProxy.createProxyServer();
  46. server.get("/api/v1", function (req, res) {
  47. delete req.headers.host;
  48. req.url = req.url.replace('/api/v1', '/');
  49. apiProxy.web(req, res, { target: 'http://private-XXXX.apiary-mock.com/' });
  50. });
  51.  
  52. var proxyOptions = {
  53. ignorePath: true
  54. };
  55.  
  56. var apiProxy = httpProxy.createProxyServer(proxyOptions);
Add Comment
Please, Sign In to add comment