9
0
mirror of https://github.com/donlon/cloudflare-error-page.git synced 2025-12-22 16:29:29 +00:00

1 Commits

Author SHA1 Message Date
Anthony Donlon
5a633b5958 readme: update description for JavaScript/NodeJS 2025-12-23 00:11:19 +08:00
3 changed files with 48 additions and 7 deletions

View File

@@ -66,12 +66,52 @@ You can also see live demo [here](https://virt.moe/cferr/examples/default).
A demo server using Flask is also available in [flask_demo.py](examples/flask_demo.py).
### Node.js/NPM
### JavaScript/NodeJS
A Node.js package is available in [nodejs](nodejs) folder. However currently it supports only Node.js but not web browsers,
and we plan to refactor it into a shared package, so it can work in both environments.
Install the `cloudflare-error-page` package using npm:
(Thanks [@junduck](https://github.com/junduck) for creating this.)
``` Bash
npm install cloudflare-error-page
```
The following example demonstrates how to create a simple Express server and return the error page to visitors.
``` JavaScript
import express from 'express';
import { render as render_cf_error_page } from 'cloudflare-error-page';
const app = express();
const port = 3000;
// Define a route for GET requests to the root URL
app.get('/', (req, res) => {
res.status(500).send(render_cf_error_page({
title: "Internal server error",
// Browser status is ok
browser_status: {
status: 'ok',
},
// Cloudflare status is error
cloudflare_status: {
status: 'error',
status_text: 'Error',
},
// Host status is also ok
host_status: {
status: 'ok',
location: 'example.com',
},
error_source: "cloudflare",
}));
});
// Start the server and listen on the specified port
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
```
(Thanks [@junduck](https://github.com/junduck) for creating the original NodeJS version.)
### PHP

View File

@@ -11,7 +11,7 @@ npm install cloudflare-error-page
Or install from GitHub:
```bash
npm install git+https://github.com/donlon/cloudflare-error-page.git#main:nodejs
npm install git+https://github.com/donlon/cloudflare-error-page.git#javascriptnodejs
```
## Quick Start
@@ -34,7 +34,7 @@ fs.writeFileSync('error.html', errorPage);
## API Reference
### `render(params: ErrorPageParams, allowHtml?: boolean): string`
### `render(params: ErrorPageParams, allowHtml?: boolean, moreArgs?: { [name: string]: any; }): string`
Generates an HTML error page based on the provided parameters.
@@ -42,6 +42,7 @@ Generates an HTML error page based on the provided parameters.
- `params`: An object containing error page configuration
- `allowHtml` (optional): Whether to allow HTML in `what_happened` and `what_can_i_do` fields. Default: `true`
- `moreArgs` (optional): More arguments passed to the ejs template
#### ErrorPageParams Interface

View File

@@ -70,7 +70,7 @@ function genHexString(digits: number): string {
* Render a customized Cloudflare error page
* @param params - The parameters for the error page
* @param allowHtml - Whether to allow HTML in what_happened and what_can_i_do fields (default: true)
* @param moreArgs - More arguments passed to ejs template
* @param moreArgs - More arguments passed to the ejs template
* @returns The rendered HTML string
*/
export function render(