npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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

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 });
});