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-file

v1.0.0

Published

file serving with advanced options for caching, headers, and error handling, compatible with Express middleware.

Downloads

10

Readme

als-send-file

als-send-file is a Node.js library designed to facilitate file serving with custom options to easily serve files with detailed control over caching, headers, and error handling. It can be used as middleware in Express.js and similar frameworks.

Installation

Install the package via npm:

npm install als-send-file

And import:

const { fileHandler, sendFileMw } = require('als-send-file');

Middleware usage sendFileMw

The sendFileMw function creates middleware that adds a sendFile method to the response object (res.sendFile). This method can be used within your route handlers to serve files with specific options.

Parameters

  • root: The root directory from which files will be served. Must be a valid string path.
  • httpErrorHandler: A function to handle HTTP errors. It should take three arguments: (res, statusCode, message).
  • $options: An object specifying default options for file serving. These options are merged with any options specified at the time of calling res.sendFile.

The middleware validates these parameters and throws an error if validation fails.

Usage of res.sendFile

const express = require('express');
const { sendFileMw } = require('als-send-file');
const app = express();

app.use(sendFileMw(__dirname, (res, code, msg) => {
  res.status(code).send(msg);
}));

app.get('/file/:name', (req, res) => {
  res.sendFile(req.params.name, { download: true, noCache: true });
});

app.listen(3000, () => {
  console.log('Server is running on http://localhost:3000');
});

Function fileHandler usage

fileHandler directly handles file serving with detailed options.

Parameters and Validation

  • req: The HTTP request object.
  • res: The HTTP response object.
  • filePath: Path to the file to be served.
  • options: Object containing options that affect headers and handling behaviors:
    • download (Boolean, default: false): Sets Content-Disposition to attachment for downloads or inline to open in the browser.
    • charset (String, optional): Adds charset to Content-Type.
    • etag (Boolean, default: true): Adds Last-Modified and ETag for client-side caching.
    • maxAge (Number, optional): Specifies the maximum amount of time (in seconds) that the resource is considered fresh.
    • noCache (Boolean, default: false): Requires that all copies of the response be revalidated with the server before use.
    • noStore (Boolean, default: false): Prohibits any caching of the response.
    • public (String, optional): Indicates where the response may be cached (in public proxies or only in the user’s browser). Defaults to public if any other value is passed.
  • httpErrorHandler: A function to handle HTTP errors. It should take three arguments: (res, statusCode, message).

Example Usage

const http = require('http');
const { fileHandler } = require('als-send-file');
const server = http.createServer((req, res) => {
   const httpErrHandler = (res, code, msg) => {
      res.status(code).send(msg);
   }
   fileHandler(req, res, 'path/to/file.txt', { download: true }, httpErrHandler);
});

server.listen(3000);

Expected Results

When using fileHandler, the function sets appropriate HTTP headers based on the options provided and handles file serving. The possible HTTP statuses returned include:

  • 200 OK: File served successfully.
  • 404 Not Found: File not found.
  • 500 Internal Server Error: Errors during file serving.
  • 304 Not Modified: File not modified (ETag matches).
  • 206 Partial Content: Partial file data returned for range requests.
  • 416 Range Not Satisfiable: Invalid range requested.

The function returns a promise, facilitating advanced handling and integration within asynchronous functions.