express-html
v1.2.4
Published
Middleware for serving HTML content in Express applications.
Maintainers
Readme
express-html
Canonical URL:
https://alexstevovich.com/a/express-html-nodejs
Software URL:
https://midnightcitylights.com/software/express-html-nodejs
express-html is a simple middleware for Express that allows you to serve HTML content in a clean and flexible way, supporting both static and dynamically-generated HTML.
Note: This package is a community-developed middleware for the Express framework and is not officially endorsed or maintained by the Express team.
Installation
npm install express-htmlExample
import express from 'express';
import { htmlHandler } from 'express-html';
const app = express();
// Serve a simple "Hello, World!" HTML response
app.get('/', htmlHandler('<html><body><h1>Hello, World!</h1></body></html>'));
app.listen(3000, () => {
console.log("Server running on http://localhost:3000");
});htmlHandler Function
htmlHandler is a middleware that sends an HTML response. It can accept either:
- A static HTML string, or
- A function that returns an HTML string asynchronously.
The function is structured as follows:
htmlOrFn: A string of HTML or a function that returns HTML.status: HTTP status code (default is 200).
Notes:
- The function checks if
htmlOrFnis a function and invokes it to generate the HTML content if necessary. - If the HTML returned is not a string, a
TypeErrorwill be thrown. - The response is sent with
Content-Type: text/html; charset=utf-8.
License
Licensed under the MIT License.
