als-send
v1.0.1
Published
An efficient middleware for Node.js that handles response sending with automated ETag generation, GZIP compression, and content type management
Downloads
8
Maintainers
Readme
als-send
The als-send
library provides a flexible and easy-to-use solution for handling HTTP responses with built-in support for ETag generation, GZIP compression, and content type handling. It can be seamlessly integrated with Node.js HTTP servers and frameworks like Express, offering automatic content negotiation and performance optimizations through HTTP caching and compression.
Key Features
- ETag Generation: Automatically generates and manages ETags to facilitate HTTP caching, reducing the need to resend data that the client already has.
- GZIP Compression: Dynamically compresses response data based on the client's
Accept-Encoding
headers, which can significantly reduce the amount of data transmitted over the network. - Content-Type Handling: Automatically determines and sets the
Content-Type
header based on the type of the response data, supporting plain text, HTML, and JSON out of the box. - Error Handling: Customizable error handling to manage errors gracefully during the request/response lifecycle.
Installation
To install als-send
, use npm:
npm install als-send
Basic Usage
With Express
Here's how you can integrate als-send
with an Express application:
const express = require("express");
const sendMw = require("als-send");
const app = express();
app.use(sendMw());
app.get("/", (req, res) => {
res.send({ message: "Hello, JSON!" });
});
app.listen(3000);
With Node.js HTTP Server
For use with the native HTTP server module:
const http = require("http");
const { send } = require("als-send");
const server = http.createServer((req, res) => {
send(req, res, "Hello, world!");
});
server.listen(3000);
Configuration
The sendMw
middleware function accepts an options
object which allows you to customize its behavior:
logger = console.error, httpErrorHandler = defHandler
const options = {
etagmin: 1024, // Minimum size in bytes for ETag generation, default:100*1024
gzipmin: 5120, // Minimum size in bytes for GZIP compression, default:1024*1024
logger: console.error, // Custom logger function, default:console.error
httpErrorHandler: (res, code, msg) => {
// Custom error handler
res.status(code).send({ error: msg });
}, // default require(als-http-error).httpErrorHandler
};
app.use(sendMw(options));
httpErrorHandler Function
The httpErrorHandler
is a function that you can define to customize how HTTP errors are handled. It expects the following parameters:
res
: The response object.code
: The HTTP status code to be sent.msg
: The error message that should be included in the response.
This function should handle ending the response, ensuring that all errors are reported back to the client appropriately.
More Examples
Custom Error Handling
Customizing how errors are handled during response sending:
const errorHandler = (res, code, msg) => {
res.writeHead(code, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: msg }));
};
app.use(sendMw({ httpErrorHandler: errorHandler }));
Using Different Settings per Route
Applying different settings for specific routes:
app.get("/large-response", (req, res) => {
const options = { gzipmin: 10240 }; // Increase minimum size for GZIP
res.send = sendMw.send(req, res, options);
res.send({ data: largeDataObject });
});