express.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. const _express = require('express');
  2. const _webapp = _express();
  3. // use require('http-error-pages') for regular apps!
  4. const _httpErrorPages = require('../lib/main');
  5. async function bootstrap(){
  6. // demo handler
  7. _webapp.get('/', function(req, res){
  8. res.type('.txt').send('HttpErrorPages Demo');
  9. });
  10. // throw an 403 error
  11. _webapp.get('/my403error', function(req, res, next){
  12. const myError = new Error();
  13. myError.status = 403;
  14. next(myError);
  15. });
  16. // throw an internal error
  17. _webapp.get('/500', function(req, res){
  18. throw new Error('Server Error');
  19. });
  20. // use http error pages handler (final statement!)
  21. // because of the asynchronous file-loaders, wait until it has been executed
  22. await _httpErrorPages.express(_webapp, {
  23. lang: 'en_US',
  24. footer: 'Hello <strong>World</strong>'
  25. });
  26. // start service
  27. _webapp.listen(8888);
  28. }
  29. // invoke bootstrap operation
  30. bootstrap()
  31. .then(function(){
  32. console.log('Running Demo on Port 8888');
  33. })
  34. .catch(function(e){
  35. console.error(e);
  36. });