diff --git a/README.md b/README.md index 86c0bcb..de97960 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/javascript/README.md b/javascript/README.md index 3d37467..3626419 100644 --- a/javascript/README.md +++ b/javascript/README.md @@ -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 diff --git a/javascript/src/index.ts b/javascript/src/index.ts index 6a96a97..8c2d99c 100644 --- a/javascript/src/index.ts +++ b/javascript/src/index.ts @@ -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(