项目作者: HttpErrorPages

项目描述 :
:fast_forward: Simple HTTP Error Page Generator
高级语言: JavaScript
项目地址: git://github.com/HttpErrorPages/HttpErrorPages.git
创建时间: 2015-01-11T16:24:38Z
项目社区:https://github.com/HttpErrorPages/HttpErrorPages

开源协议:MIT License

下载


nginx |
Apache HTTPD |
Lighttpd |
express.js |
koa.js |
Caddy |
Customization

Simple HttpErrorPages

Simple HTTP Error Page Generator. Create a bunch of custom error pages - suitable to use with Lighttpd, Nginx, expressjs, koajs ,Apache-Httpd or any other Webserver.

Screenshot

Features

  • Static pages (for webservers)
  • Multi-Language (i18n) support
  • Generator script to customize pages
  • Native express.js middleware
  • Native koa.js middleware

Demo

Download

Just clone/download the git repository or use the prebuild packages (only the generated html files are included)

Download Prebuild Packages (Pages only, en_US)

NGINX Integration

NGINX supports custom error-pages using multiple error_page directives.

File: default.conf

Example - assumes HttpErrorPages are located into /var/ErrorPages/.

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. root /var/www;
  5. index index.html;
  6. location / {
  7. try_files $uri $uri/ =404;
  8. # add one directive for each http status code
  9. error_page 400 /ErrorPages/HTTP400.html;
  10. error_page 401 /ErrorPages/HTTP401.html;
  11. error_page 402 /ErrorPages/HTTP402.html;
  12. error_page 403 /ErrorPages/HTTP403.html;
  13. error_page 404 /ErrorPages/HTTP404.html;
  14. error_page 500 /ErrorPages/HTTP500.html;
  15. error_page 501 /ErrorPages/HTTP501.html;
  16. error_page 502 /ErrorPages/HTTP502.html;
  17. error_page 503 /ErrorPages/HTTP503.html;
  18. }
  19. # redirect the virtual ErrorPages path the real path
  20. location /ErrorPages/ {
  21. alias /var/ErrorPages/;
  22. internal;
  23. }

Apache Httpd Integration

Apache Httpd 2.x supports custom error-pages using multiple ErrorDocument directives.

File: httpd.conf or .htaccess

Example - assumes HttpErrorPages are located into your document root /var/www/...docroot../ErrorPages.

  1. ErrorDocument 400 /ErrorPages/HTTP400.html
  2. ErrorDocument 401 /ErrorPages/HTTP401.html
  3. ErrorDocument 403 /ErrorPages/HTTP403.html
  4. ErrorDocument 404 /ErrorPages/HTTP404.html
  5. ErrorDocument 500 /ErrorPages/HTTP500.html
  6. ErrorDocument 501 /ErrorPages/HTTP501.html
  7. ErrorDocument 502 /ErrorPages/HTTP502.html
  8. ErrorDocument 503 /ErrorPages/HTTP503.html

Lighttpd Integration

Lighttpd supports custom error-pages using the server.errorfile-prefix directive.

File: lighttpd.conf

Example - assumes HttpErrorPages are located into /var/www/ErrorPages/.

  1. server.errorfile-prefix = "/var/www/ErrorPages/HTTP"

expressjs Integration

HttpErrorPages are available as NPM-Package - just install http-error-pages via npm/yarn

Installation

  1. yarn add http-error-pages

Example

A ready-to-use example can be found in examples/express.js

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

Options

Syntax: Promise _httpErrorPages.express(expressWebapp [, options:Object])

  • template (type:string) - the path to a custom EJS template used to generate the pages. default assets/template.ejs
  • css (type:string) - the path to a precompiled CSS file injected into the page. default assets/layout.css
  • lang (type:string) - language definition which should be used (available in the i18n/ directory). default en_US
  • payload (type:object) - additional variables available within the template
  • payload.footer (type:string) - optional page footer content (html allowed). default null
  • filter (type:function) - filter callback to manipulate the variables before populated within the template
  • onError (type:function) - simple debug handler to print errors to the console (not to be used in production!)

koajs Integration

HttpErrorPages are available as NPM-Package - just install http-error-pages via npm/yarn

Installation

  1. yarn add http-error-pages

Example

A ready-to-use example can be found in examples/koa.js.
Keep in mind that the following example has to be executed within an async context!

  1. const _koa = require('koa');
  2. const _webapp = new _koa();
  3. const _httpErrorPages = require('http-error-pages');
  4. // use http error pages handler (INITIAL statement!)
  5. // because of the asynchronous file-loaders, wait until it has been executed - it returns an async handler
  6. _webapp.use(await _httpErrorPages.koa({
  7. lang: 'en_US',
  8. payload: {
  9. footer: 'Hello <strong>World</strong>',
  10. myvar: 'hello world'
  11. }
  12. }));
  13. // add other middleware handlers
  14. _webapp.use(async (ctx, next) => {
  15. if (ctx.path == '/'){
  16. ctx.type = 'text';
  17. ctx.body = 'HttpErrorPages Demo';
  18. }else{
  19. return next();
  20. }
  21. });
  22. // start service
  23. _webapp.listen(8888);

Options

Syntax: Promise _httpErrorPages.koa([options:Object])

  • template (type:string) - the path to a custom EJS template used to generate the pages. default assets/template.ejs
  • css (type:string) - the path to a precompiled CSS file injected into the page. default assets/layout.css
  • lang (type:string) - language definition which should be used (available in the i18n/ directory). default en_US
  • payload (type:object) - additional variables available within the template
  • payload.footer (type:string) - optional page footer content (html allowed). default null
  • filter (type:function) - filter callback to manipulate the variables before populated within the template
  • onError (type:function) - simple debug handler to print errors to the console (not to be used in production!)

Caddy Integration

Caddy supports custom error-pages using errors directive.

File: Caddyfile

Example - assumes HttpErrorPages are located into /var/www/error.

  1. www.yoursite.com {
  2. // Other configurations
  3. errors {
  4. 404 /var/www/error/HTTP404.html
  5. }
  6. // Other configurations
  7. }

Customization

First of all, clone
or download the http-error-pages repository.

Install Dependencies

You have to install the node dev dependencies to build the pages:

  1. # run the yarn command within the cloned repository
  2. yarn install
  3. # or if you more familiar with npm..
  4. npm install

To customize the pages, you can edit any of the template files and finally run the generator-script.
All generated html files are located into the dist/ directory by default.

If you wan’t to add custom pages/additional error-codes, just put a new entry into the i18n/pages-en_US.json file (its recommended to copy the file).
The generator-script will process each entry and generates an own page.

Files

Change page styles

To modify the page styles, just edit the SCSS based layout assets/layout.scss and finally run gulp to generate the css code.
The new layout file is stored in assets/layout.css - run the page generator to create the pages.

Example

  1. # start gulp sccs via npm
  2. $ npm run gulp
  3. > http-error-pages@0.6.0 gulp HttpErrorPages
  4. > gulp
  5. [08:40:33] Using gulpfile HttpErrorPages/gulpfile.js
  6. [08:40:33] Starting 'sass'...
  7. [08:40:34] Finished 'sass' after 108 ms
  8. [08:40:34] Starting 'default'...
  9. [08:40:34] Finished 'default' after 40 μs
  10. # generate http-error-pages using modified stylesheet
  11. $ npm run static
  12. > http-error-pages@0.6.0 static HttpErrorPages
  13. > node bin/generator.js static
  14. Paths
  15. |- Config: HttpErrorPages/config.json
  16. |- Template: HttpErrorPages/assets/template.ejs
  17. |- Styles: HttpErrorPages/assets/layout.css
  18. |- Pages: HttpErrorPages/i18n/pages-en_US.json
  19. Generating static pages
  20. |- Page <HTTP404.html>
  21. |- Page <HTTP403.html>
  22. |- Page <HTTP400.html>
  23. |- Page <HTTP500.html>
  24. |- Page <HTTP501.html>
  25. |- Page <HTTP502.html>
  26. |- Page <HTTP520.html>
  27. |- Page <HTTP503.html>
  28. |- Page <HTTP521.html>
  29. |- Page <HTTP533.html>
  30. |- Page <HTTP401.html>
  31. Static files generated

Multi language (i18n)

To use a different language just provide a custom page definition - in case the file is located in i18n you can use the --lang option

Example

  1. $ npm run static -- --lang pt_BR
  2. > http-error-pages@0.6.0 static HttpErrorPages
  3. > node bin/generator.js static "--lang" "pt_BR"
  4. Paths
  5. |- Config: HttpErrorPages/config.json
  6. |- Template: HttpErrorPages/assets/template.ejs
  7. |- Styles: HttpErrorPages/assets/layout.css
  8. |- Pages: HttpErrorPages/i18n/pages-pt_BR.json
  9. Generating static pages
  10. |- Page <HTTP404.html>
  11. |- Page <HTTP400.html>
  12. |- Page <HTTP401.html>
  13. |- Page <HTTP403.html>
  14. |- Page <HTTP500.html>
  15. |- Page <HTTP501.html>
  16. |- Page <HTTP502.html>
  17. |- Page <HTTP520.html>
  18. |- Page <HTTP503.html>
  19. |- Page <HTTP521.html>
  20. |- Page <HTTP533.html>
  21. Static files generated

Add custom pages

Create custom error codes/pages used by e.g. CloudFlare

Example

  1. // webserver origin error
  2. "520": {
  3. "title": "Origin Error - Unknown Host",
  4. "message": "The requested hostname is not routed. Use only hostnames to access resources."
  5. },
  6. // webserver down error
  7. "521": {
  8. "title": "Webservice currently unavailable",
  9. "message": "We've got some trouble with our backend upstream cluster.\nOur service team has been dispatched to bring it back online."
  10. },

The footer message can easily be changed/removed by editing config.json.

Example - customm footer

  1. {
  2. // Output Filename Scheme - eg. HTTP500.html
  3. "scheme": "HTTP%code%.html",
  4. // Footer content (HTML Allowed)
  5. "footer": "Contact <a href=\"mailto:info@example.org\">info@example.org</a>"
  6. }

Example - no footer

  1. {
  2. // Output Filename Scheme - eg. HTTP500.html
  3. "scheme": "HTTP%code%.html"
  4. }

Placeholders/Variables

The following set of variables is exposed to the ejs template (404 page example):

  1. {
  2. title: 'Resource not found',
  3. message: 'The requested resource could not be found but may be available again in the future.',
  4. code: '404',
  5. language: 'en',
  6. scheme: 'HTTP%code%.html',
  7. pagetitle: "We've got some trouble | %code% - %title%",
  8. footer: 'Tech Contact <a href="mailto:info@example.org">info@example.org</a>',
  9. myvar: 'Hello World'
  10. }

To generate dynamic titles/content based on the current variable set, each variable is exposed as placeholder (surrounded by %).

You can also define custom variable within the page definitions, everything is merged togehter.

Modify the HTML template

The HTML template is based on ejs and located in assets/template.ejs - you can apply any kind of changes.

  1. <!DOCTYPE html>
  2. <html lang="<%= vars.language %>">
  3. <head>
  4. <!-- Simple HttpErrorPages | MIT License | https://github.com/HttpErrorPages -->
  5. <meta charset="utf-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <title><%= vars.pagetitle %></title>
  7. <style type="text/css"><%- vars.inlinecss %></style>
  8. </head>
  9. <body>
  10. <div class="cover"><h1><%= vars.title %> <small><%= vars.code %></small></h1><p class="lead"><%= vars.message %></p></div>
  11. <% if (vars.footer){ %><footer><p><%- vars.footer %></p></footer><% } %>
  12. </body>
  13. </html>

Command line options

The http-error-pages generator allows you to use custom template/config files directly. This is the recommended method to create full-customized pages.

  1. $ npm run static -- --help
  2. > http-error-pages@0.6.0 static HttpErrorPages
  3. > node bin/generator.js static "--help"
  4. Usage: static [options] [config]
  5. run http-error-pages generator
  6. Options:
  7. -t, --template <path> path to your custom EJS template file
  8. -s, --styles <path> path to your custom stylesheet (precompiled as CSS!)
  9. -p, --pages <path> path to your custom page definition
  10. -l, --lang <lang> the language of the default page definition
  11. -o, --out <path> output directory
  12. -h, --help output usage information

Example - use custom files

We assume you’ve created a folder named example_org which contains all relevant template files

  1. # via npm run-script (cross platform)
  2. $ npm run static -- -t example_org/template.ejs -s example_org/styles.css -p example_org/pages.json -o example_org/output
  3. # .. or directly (linux only)
  4. $ http-error-pages -t example_org/template.ejs -s example_org/styles.css -p example_org/pages.json -o example_org/output

License

HttpErrorsPages is OpenSource and licensed under the Terms of The MIT License (X11) - your’re welcome to contribute