Skip to content

Commit 0569f8d

Browse files
committed
added koa support
1 parent 6b5c030 commit 0569f8d

28 files changed

+461
-94
lines changed

CHANGES.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
CHANGELOG
22
======================================================
33

4+
Version 2.X.X
5+
------------------------------------------------------
6+
* Added [koa](http://koajs.com/) support
7+
* Added support for multiple http frameworks
8+
* Changed: **express.js** error handler is encapsulated within the `express()` function
9+
* Changed: moved middleware example to `examples/`
10+
411
Version 1.X.X
512
------------------------------------------------------
613

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (X11 License)
22

3-
Copyright (c) 2014-2017 Andi Dittrich
3+
Copyright (c) 2014-2018 Andi Dittrich
44

55
Permission is hereby granted, free of charge, to any person
66
obtaining a copy of this software and associated documentation

README.md

Lines changed: 89 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
[nginx](#nginx-integration) |
2-
[expressjs](#expressjs-integration) |
32
[Apache HTTPD](#apache-httpd-integration) |
43
[Lighttpd](#lighttpd-integration) |
4+
[express.js](#expressjs-integration) |
5+
[koa.js](#koajs-integration) |
56
[Customization](#customization)
67

78
# Simple HttpErrorPages #
8-
Simple HTTP Error Page Generator. Create a bunch of custom error pages - suitable to use with Lighttpd, Nginx, expressjs, Apache-Httpd or any other Webserver.
9+
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.
910

1011
![Screenshot](https://raw.githubusercontent.com/AndiDittrich/HttpErrorPages/master/assets/screenshot1.png)
1112

13+
## Features ##
14+
15+
* Static pages (for webservers)
16+
* Multi-Language (i18n) support
17+
* Generator script to customize pages
18+
* Native [express.js](http://expressjs.com/) middleware
19+
* Native [koa.js](http://koajs.com/) middleware
20+
1221
## Demo ##
1322
* [HTTP400](https://andidittrich.github.io/HttpErrorPages/HTTP400.html)
1423
* [HTTP401](https://andidittrich.github.io/HttpErrorPages/HTTP401.html)
@@ -72,24 +81,54 @@ server {
7281
}
7382
```
7483

84+
## Apache Httpd Integration ##
85+
[Apache Httpd 2.x](http://httpd.apache.org/) supports custom error-pages using multiple [ErrorDocument](http://httpd.apache.org/docs/2.4/mod/core.html#errordocument) directives.
86+
87+
File: `httpd.conf` or `.htaccess`
88+
89+
Example - assumes HttpErrorPages are located into your **document root** `/var/www/...docroot../ErrorPages`.
90+
91+
```ApacheConf
92+
ErrorDocument 400 /ErrorPages/HTTP400.html
93+
ErrorDocument 401 /ErrorPages/HTTP401.html
94+
ErrorDocument 403 /ErrorPages/HTTP403.html
95+
ErrorDocument 404 /ErrorPages/HTTP404.html
96+
ErrorDocument 500 /ErrorPages/HTTP500.html
97+
ErrorDocument 501 /ErrorPages/HTTP501.html
98+
ErrorDocument 502 /ErrorPages/HTTP502.html
99+
ErrorDocument 503 /ErrorPages/HTTP503.html
100+
```
101+
102+
## Lighttpd Integration ##
103+
104+
[Lighttpd](http://www.lighttpd.net/) supports custom error-pages using the [server.errorfile-prefix](http://redmine.lighttpd.net/projects/lighttpd/wiki/Server_errorfile-prefixDetails) directive.
105+
106+
File: `lighttpd.conf`
107+
108+
Example - assumes HttpErrorPages are located into `/var/www/ErrorPages/`.
109+
110+
```ApacheConf
111+
server.errorfile-prefix = "/var/www/ErrorPages/HTTP"
112+
```
113+
75114
## expressjs Integration ##
76115

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

79118
**Installation**
80119

81120
```terminal
82-
npm install http-error-pages --save
121+
yarn add http-error-pages
83122
```
84123

85124
**Example**
86125

126+
A ready-to-use example can be found in [examples/express.js](exmaples/express.js)
127+
87128
```js
88129
const _express = require('express');
89130
const _webapp = _express();
90-
91-
// use require('http-error-pages') for regular apps!
92-
const _httpErrorPages = require('./lib/error-handler');
131+
const _httpErrorPages = require('http-error-pages');
93132

94133
async function bootstrap(){
95134
// demo handler
@@ -111,7 +150,7 @@ async function bootstrap(){
111150

112151
// use http error pages handler (final statement!)
113152
// because of the asynchronous file-loaders, wait until it has been executed
114-
await _httpErrorPages(_webapp, {
153+
await _httpErrorPages.express(_webapp, {
115154
lang: 'en_US',
116155
footer: 'Hello <strong>World</strong>'
117156
});
@@ -132,43 +171,64 @@ bootstrap()
132171

133172
**Options**
134173

135-
Syntax: `Promise _httpErrorPages(expressWebapp [, options:Object])`
174+
Syntax: `Promise _httpErrorPages.express(expressWebapp [, options:Object])`
136175

137176
* `template` - the path to a custom **EJS** template used to generate the pages. default [assets/template.ejs](assets/template.ejs)
138177
* `css` - the path to a precompiled **CSS** file injected into the page. default [assets/layout.css](assets/layout.css)
139178
* `footer` - optional page footer content (html allowed). default **null**
140179
* `lang` - language definition which should be used (available in the `i18n/` directory). default **en_US**
141180

142-
## Apache Httpd Integration ##
143-
[Apache Httpd 2.x](http://httpd.apache.org/) supports custom error-pages using multiple [ErrorDocument](http://httpd.apache.org/docs/2.4/mod/core.html#errordocument) directives.
181+
## koajs Integration ##
144182

145-
File: `httpd.conf` or `.htaccess`
183+
HttpErrorPages are available as NPM-Package - just install `http-error-pages` via **npm/yarn**
146184

147-
Example - assumes HttpErrorPages are located into your **document root** `/var/www/...docroot../ErrorPages`.
185+
**Installation**
148186

149-
```ApacheConf
150-
ErrorDocument 400 /ErrorPages/HTTP400.html
151-
ErrorDocument 401 /ErrorPages/HTTP401.html
152-
ErrorDocument 403 /ErrorPages/HTTP403.html
153-
ErrorDocument 404 /ErrorPages/HTTP404.html
154-
ErrorDocument 500 /ErrorPages/HTTP500.html
155-
ErrorDocument 501 /ErrorPages/HTTP501.html
156-
ErrorDocument 502 /ErrorPages/HTTP502.html
157-
ErrorDocument 503 /ErrorPages/HTTP503.html
187+
```terminal
188+
yarn add http-error-pages
158189
```
159190

160-
## Lighttpd Integration ##
161-
162-
[Lighttpd](http://www.lighttpd.net/) supports custom error-pages using the [server.errorfile-prefix](http://redmine.lighttpd.net/projects/lighttpd/wiki/Server_errorfile-prefixDetails) directive.
191+
**Example**
163192

164-
File: `lighttpd.conf`
193+
A ready-to-use example can be found in [examples/koa.js](exmaples/koa.js).
194+
Keep in mind that the following example has to be executed within an async context!
165195

166-
Example - assumes HttpErrorPages are located into `/var/www/ErrorPages/`.
196+
```js
197+
const _koa = require('koa');
198+
const _webapp = new _koa();
199+
const _httpErrorPages = require('http-error-pages');
200+
201+
// use http error pages handler (INITIAL statement!)
202+
// because of the asynchronous file-loaders, wait until it has been executed - it returns an async handler
203+
_webapp.use(await _httpErrorPages.koa({
204+
lang: 'en_US',
205+
footer: 'Hello <strong>World</strong>'
206+
}));
207+
208+
// add other middleware handlers
209+
_webapp.use(async (ctx, next) => {
210+
if (ctx.path == '/'){
211+
ctx.type = 'text';
212+
ctx.body = 'HttpErrorPages Demo';
213+
}else{
214+
return next();
215+
}
216+
});
167217

168-
```ApacheConf
169-
server.errorfile-prefix = "/var/www/ErrorPages/HTTP"
218+
// start service
219+
_webapp.listen(8888);
170220
```
171221

222+
**Options**
223+
224+
Syntax: `Promise _httpErrorPages.koa([options:Object])`
225+
226+
* `template` - the path to a custom **EJS** template used to generate the pages. default [assets/template.ejs](assets/template.ejs)
227+
* `css` - the path to a precompiled **CSS** file injected into the page. default [assets/layout.css](assets/layout.css)
228+
* `footer` - optional page footer content (html allowed). default **null**
229+
* `lang` - language definition which should be used (available in the `i18n/` directory). default **en_US**
230+
231+
172232
## Customization ##
173233

174234
First of all, [clone](https://github.com/AndiDittrich/HttpErrorPages.git)
@@ -186,7 +246,7 @@ yarn install
186246
npm install
187247
```
188248

189-
To customize the pages, you can edit any of the template files and **finally run the generator-script**.
249+
To customize the pages, you can edit any of the [template files](assets/) and **finally run the generator-script**.
190250
All generated html files are located into the `dist/` directory by default.
191251

192252
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).

dist/HTTP500.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
<style type="text/css">/*! normalize.css v5.0.0 | MIT License | github.com/necolas/normalize.css */html{font-family:sans-serif;line-height:1.15;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%}body{margin:0}article,aside,footer,header,nav,section{display:block}h1{font-size:2em;margin:.67em 0}figcaption,figure,main{display:block}figure{margin:1em 40px}hr{box-sizing:content-box;height:0;overflow:visible}pre{font-family:monospace,monospace;font-size:1em}a{background-color:transparent;-webkit-text-decoration-skip:objects}a:active,a:hover{outline-width:0}abbr[title]{border-bottom:none;text-decoration:underline;text-decoration:underline dotted}b,strong{font-weight:inherit}b,strong{font-weight:bolder}code,kbd,samp{font-family:monospace,monospace;font-size:1em}dfn{font-style:italic}mark{background-color:#ff0;color:#000}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}audio,video{display:inline-block}audio:not([controls]){display:none;height:0}img{border-style:none}svg:not(:root){overflow:hidden}button,input,optgroup,select,textarea{font-family:sans-serif;font-size:100%;line-height:1.15;margin:0}button,input{overflow:visible}button,select{text-transform:none}[type=reset],[type=submit],button,html [type=button]{-webkit-appearance:button}[type=button]::-moz-focus-inner,[type=reset]::-moz-focus-inner,[type=submit]::-moz-focus-inner,button::-moz-focus-inner{border-style:none;padding:0}[type=button]:-moz-focusring,[type=reset]:-moz-focusring,[type=submit]:-moz-focusring,button:-moz-focusring{outline:1px dotted ButtonText}fieldset{border:1px solid silver;margin:0 2px;padding:.35em .625em .75em}legend{box-sizing:border-box;color:inherit;display:table;max-width:100%;padding:0;white-space:normal}progress{display:inline-block;vertical-align:baseline}textarea{overflow:auto}[type=checkbox],[type=radio]{box-sizing:border-box;padding:0}[type=number]::-webkit-inner-spin-button,[type=number]::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px}[type=search]::-webkit-search-cancel-button,[type=search]::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}details,menu{display:block}summary{display:list-item}canvas{display:inline-block}template{display:none}[hidden]{display:none}/*! Simple HttpErrorPages | MIT X11 License | https://github.com/AndiDittrich/HttpErrorPages */body,html{width:100%;height:100%;background-color:#21232a}body{color:#fff;text-align:center;text-shadow:0 2px 4px rgba(0,0,0,.5);padding:0;min-height:100%;-webkit-box-shadow:inset 0 0 100px rgba(0,0,0,.8);box-shadow:inset 0 0 100px rgba(0,0,0,.8);display:table;font-family:"Open Sans",Arial,sans-serif}h1{font-family:inherit;font-weight:500;line-height:1.1;color:inherit;font-size:36px}h1 small{font-size:68%;font-weight:400;line-height:1;color:#777}a{text-decoration:none;color:#fff;font-size:inherit;border-bottom:dotted 1px #707070}.lead{color:silver;font-size:21px;line-height:1.4}.cover{display:table-cell;vertical-align:middle;padding:0 20px}footer{position:fixed;width:100%;height:40px;left:0;bottom:0;color:#a0a0a0;font-size:14px}</style>
88
</head>
99
<body>
10-
<div class="cover"><h1>Webservice currently unavailable <small>Error 500</small></h1><p class="lead">An unexpected condition was encountered.
11-
Our service team has been dispatched to bring it back online.</p></div>
10+
<div class="cover"><h1>Webservice currently unavailable <small>Error 500</small></h1><p class="lead">An unexpected condition was encountered.<br />Our service team has been dispatched to bring it back online.</p></div>
1211

1312
</body>
1413
</html>

dist/HTTP502.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
<style type="text/css">/*! normalize.css v5.0.0 | MIT License | github.com/necolas/normalize.css */html{font-family:sans-serif;line-height:1.15;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%}body{margin:0}article,aside,footer,header,nav,section{display:block}h1{font-size:2em;margin:.67em 0}figcaption,figure,main{display:block}figure{margin:1em 40px}hr{box-sizing:content-box;height:0;overflow:visible}pre{font-family:monospace,monospace;font-size:1em}a{background-color:transparent;-webkit-text-decoration-skip:objects}a:active,a:hover{outline-width:0}abbr[title]{border-bottom:none;text-decoration:underline;text-decoration:underline dotted}b,strong{font-weight:inherit}b,strong{font-weight:bolder}code,kbd,samp{font-family:monospace,monospace;font-size:1em}dfn{font-style:italic}mark{background-color:#ff0;color:#000}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}audio,video{display:inline-block}audio:not([controls]){display:none;height:0}img{border-style:none}svg:not(:root){overflow:hidden}button,input,optgroup,select,textarea{font-family:sans-serif;font-size:100%;line-height:1.15;margin:0}button,input{overflow:visible}button,select{text-transform:none}[type=reset],[type=submit],button,html [type=button]{-webkit-appearance:button}[type=button]::-moz-focus-inner,[type=reset]::-moz-focus-inner,[type=submit]::-moz-focus-inner,button::-moz-focus-inner{border-style:none;padding:0}[type=button]:-moz-focusring,[type=reset]:-moz-focusring,[type=submit]:-moz-focusring,button:-moz-focusring{outline:1px dotted ButtonText}fieldset{border:1px solid silver;margin:0 2px;padding:.35em .625em .75em}legend{box-sizing:border-box;color:inherit;display:table;max-width:100%;padding:0;white-space:normal}progress{display:inline-block;vertical-align:baseline}textarea{overflow:auto}[type=checkbox],[type=radio]{box-sizing:border-box;padding:0}[type=number]::-webkit-inner-spin-button,[type=number]::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px}[type=search]::-webkit-search-cancel-button,[type=search]::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}details,menu{display:block}summary{display:list-item}canvas{display:inline-block}template{display:none}[hidden]{display:none}/*! Simple HttpErrorPages | MIT X11 License | https://github.com/AndiDittrich/HttpErrorPages */body,html{width:100%;height:100%;background-color:#21232a}body{color:#fff;text-align:center;text-shadow:0 2px 4px rgba(0,0,0,.5);padding:0;min-height:100%;-webkit-box-shadow:inset 0 0 100px rgba(0,0,0,.8);box-shadow:inset 0 0 100px rgba(0,0,0,.8);display:table;font-family:"Open Sans",Arial,sans-serif}h1{font-family:inherit;font-weight:500;line-height:1.1;color:inherit;font-size:36px}h1 small{font-size:68%;font-weight:400;line-height:1;color:#777}a{text-decoration:none;color:#fff;font-size:inherit;border-bottom:dotted 1px #707070}.lead{color:silver;font-size:21px;line-height:1.4}.cover{display:table-cell;vertical-align:middle;padding:0 20px}footer{position:fixed;width:100%;height:40px;left:0;bottom:0;color:#a0a0a0;font-size:14px}</style>
88
</head>
99
<body>
10-
<div class="cover"><h1>Webservice currently unavailable <small>Error 502</small></h1><p class="lead">We&#39;ve got some trouble with our backend upstream cluster.
11-
Our service team has been dispatched to bring it back online.</p></div>
10+
<div class="cover"><h1>Webservice currently unavailable <small>Error 502</small></h1><p class="lead">We&#39;ve got some trouble with our backend upstream cluster.<br />Our service team has been dispatched to bring it back online.</p></div>
1211

1312
</body>
1413
</html>

dist/HTTP503.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
<style type="text/css">/*! normalize.css v5.0.0 | MIT License | github.com/necolas/normalize.css */html{font-family:sans-serif;line-height:1.15;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%}body{margin:0}article,aside,footer,header,nav,section{display:block}h1{font-size:2em;margin:.67em 0}figcaption,figure,main{display:block}figure{margin:1em 40px}hr{box-sizing:content-box;height:0;overflow:visible}pre{font-family:monospace,monospace;font-size:1em}a{background-color:transparent;-webkit-text-decoration-skip:objects}a:active,a:hover{outline-width:0}abbr[title]{border-bottom:none;text-decoration:underline;text-decoration:underline dotted}b,strong{font-weight:inherit}b,strong{font-weight:bolder}code,kbd,samp{font-family:monospace,monospace;font-size:1em}dfn{font-style:italic}mark{background-color:#ff0;color:#000}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}audio,video{display:inline-block}audio:not([controls]){display:none;height:0}img{border-style:none}svg:not(:root){overflow:hidden}button,input,optgroup,select,textarea{font-family:sans-serif;font-size:100%;line-height:1.15;margin:0}button,input{overflow:visible}button,select{text-transform:none}[type=reset],[type=submit],button,html [type=button]{-webkit-appearance:button}[type=button]::-moz-focus-inner,[type=reset]::-moz-focus-inner,[type=submit]::-moz-focus-inner,button::-moz-focus-inner{border-style:none;padding:0}[type=button]:-moz-focusring,[type=reset]:-moz-focusring,[type=submit]:-moz-focusring,button:-moz-focusring{outline:1px dotted ButtonText}fieldset{border:1px solid silver;margin:0 2px;padding:.35em .625em .75em}legend{box-sizing:border-box;color:inherit;display:table;max-width:100%;padding:0;white-space:normal}progress{display:inline-block;vertical-align:baseline}textarea{overflow:auto}[type=checkbox],[type=radio]{box-sizing:border-box;padding:0}[type=number]::-webkit-inner-spin-button,[type=number]::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px}[type=search]::-webkit-search-cancel-button,[type=search]::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}details,menu{display:block}summary{display:list-item}canvas{display:inline-block}template{display:none}[hidden]{display:none}/*! Simple HttpErrorPages | MIT X11 License | https://github.com/AndiDittrich/HttpErrorPages */body,html{width:100%;height:100%;background-color:#21232a}body{color:#fff;text-align:center;text-shadow:0 2px 4px rgba(0,0,0,.5);padding:0;min-height:100%;-webkit-box-shadow:inset 0 0 100px rgba(0,0,0,.8);box-shadow:inset 0 0 100px rgba(0,0,0,.8);display:table;font-family:"Open Sans",Arial,sans-serif}h1{font-family:inherit;font-weight:500;line-height:1.1;color:inherit;font-size:36px}h1 small{font-size:68%;font-weight:400;line-height:1;color:#777}a{text-decoration:none;color:#fff;font-size:inherit;border-bottom:dotted 1px #707070}.lead{color:silver;font-size:21px;line-height:1.4}.cover{display:table-cell;vertical-align:middle;padding:0 20px}footer{position:fixed;width:100%;height:40px;left:0;bottom:0;color:#a0a0a0;font-size:14px}</style>
88
</head>
99
<body>
10-
<div class="cover"><h1>Webservice currently unavailable <small>Error 503</small></h1><p class="lead">We&#39;ve got some trouble with our backend upstream cluster.
11-
Our service team has been dispatched to bring it back online.</p></div>
10+
<div class="cover"><h1>Webservice currently unavailable <small>Error 503</small></h1><p class="lead">We&#39;ve got some trouble with our backend upstream cluster.<br />Our service team has been dispatched to bring it back online.</p></div>
1211

1312
</body>
1413
</html>

0 commit comments

Comments
 (0)