index.js 738 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict'
  2. const http = require('http')
  3. const Youch = require('../src/Youch')
  4. class HttpException extends Error {
  5. constructor (...args) {
  6. super(...args)
  7. this.name = this.constructor.name
  8. }
  9. }
  10. function foo () {
  11. const error = new HttpException('Some weird error')
  12. error.status = 503
  13. throw error
  14. }
  15. http.createServer((req, res) => {
  16. let youch = null
  17. try {
  18. foo()
  19. } catch (e) {
  20. youch = new Youch(e, req)
  21. }
  22. youch
  23. .toHTML()
  24. .then((response) => {
  25. res.writeHead(200, {'content-type': 'text/html'})
  26. res.write(response)
  27. res.end()
  28. }).catch((error) => {
  29. res.writeHead(500)
  30. res.write(error.message)
  31. res.end()
  32. })
  33. }).listen(8000, () => {
  34. console.log('listening to port 8000')
  35. })