PageRenderTime 52ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/browser-sync/node_modules/http-proxy/examples/http/error-handling.js

https://github.com/bryant-pham/BusBro
JavaScript | 63 lines | 18 code | 5 blank | 40 comment | 0 complexity | 9ce3d132dca233873ba80647d51a4e0b MD5 | raw file
Possible License(s): GPL-3.0, MIT, JSON, GPL-2.0, BSD-3-Clause, Apache-2.0
  1. /*
  2. error-handling.js: Example of handle erros for HTTP and WebSockets
  3. Copyright (c) Nodejitsu 2013
  4. Permission is hereby granted, free of charge, to any person obtaining
  5. a copy of this software and associated documentation files (the
  6. "Software"), to deal in the Software without restriction, including
  7. without limitation the rights to use, copy, modify, merge, publish,
  8. distribute, sublicense, and/or sell copies of the Software, and to
  9. permit persons to whom the Software is furnished to do so, subject to
  10. the following conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. var util = require('util'),
  22. colors = require('colors'),
  23. http = require('http'),
  24. httpProxy = require('../../lib/http-proxy');
  25. //
  26. // HTTP Proxy Server
  27. //
  28. var proxy = httpProxy.createProxyServer({target:'http://localhost:9000', ws:true});
  29. //
  30. // Example of error handling
  31. //
  32. function requestHandler(req, res) {
  33. // Pass a callback to the web proxy method
  34. // and catch the error there.
  35. proxy.web(req, res, function (err) {
  36. // Now you can get the err
  37. // and handle it by your self
  38. // if (err) throw err;
  39. res.writeHead(502);
  40. res.end("There was an error proxying your request");
  41. });
  42. // In a websocket request case
  43. req.on('upgrade', function (req, socket, head) {
  44. proxy.ws(req, socket, head, function (err) {
  45. // Now you can get the err
  46. // and handle it by your self
  47. // if (err) throw err;
  48. socket.close();
  49. })
  50. })
  51. }
  52. http.createServer(requestHandler).listen(8000);
  53. util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);