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-static-routes

v2.0.0

Published

A versatile static file server and middleware with built-in caching, ETag, charset encoding, and dynamic file serving capabilities for Node.js applications.

Downloads

89

Readme

als-static-routes

als-static-routes is a utility for handling static files in web applications, enabling efficient static content management. It integrates seamlessly with frameworks like Express, but can also operate as a standalone static file server.

Key Features

  • Efficient Caching: once file used, it cached (effective cache management by memory limit with als-uni-store).
  • ETag Handling: Minimizes server load and enhances client load times by utilizing ETags.
  • Dynamic Charset Management: Automates character set determination to ensure text files are compatible with various clients.
  • Flexible Content Delivery: Supports direct file downloads and automated handling of HTML index files.
  • Real-time Configuration: Allows live changes to how files are served without needing to restart the server.

Installation

Install the package using npm:

npm install als-static-routes

Quick Start

Standalone Static Server

const StaticRouter = require('als-static-routes');
const staticRouter = new StaticRouter(__dirname);
staticRouter.add('/', 'public');

const http = require('http');
const server = http.createServer(staticRouter.handler);
server.listen(3000);

Or

const StaticRouter = require('als-static-routes');
const staticRouter = new StaticRouter(__dirname);
staticRouter.add('/', 'public')

const http = require('http');
const server = http.createServer((req,res) => {
   staticRouter.handler(req,res,(req,res) => {
      // if no static files response with something else
      res.end('Some another route')
   })
})
server.listen(3000);

Integration with Express

const StaticRouter = require('als-static-routes');
const staticRouter = new StaticRouter(__dirname);
staticRouter.add('/', 'public');

const express = require('express');
const app = express();
app.use(staticRouter.handler);
app.listen(3000);

API Reference

Constructor

Create a new router:

const options = {
   index: true, // Serve 'index.html' files for directory routes
   download: false, // Send files with 'Content-Disposition: attachment'
   etag: true, // Enable ETag header for caching optimization
   charset: true // Append charset to 'Content-Type' header based on file content and mime type
};

const router = new StaticRouter(rootDir, options);

add(url, path, [options])

Add routes for serving files or directories:

  • url: URL path to serve the file or directory.
  • path: Path relative to rootDir.
  • options: Overrides constructor options for specific routes.

Throwing errors when:

  • path not exists
  • something wrong with directory reading

Examples:

router.add('/','public',{index:false})
router.add('/route-to-file','/some/file.txt')
router.add('/file-for-download','/some/file.txt',{download:true}) // download link

Each add, updates previous adds.

remove(path)

You can remove specific route or group of routes, by passing as argument url for routes which starts with this url.

Remove a file or directory from being served:

await router.remove('/path/to/resource');

handler(req, res, [next])

Handler function to integrate with Node.js HTTP server or Express:

const server = http.createServer((req, res) => {
   router.handler(req, res, () => {
      res.writeHead(404);
      res.end('Not Found');
   });
});

If next is not provided, a default 404 handler is used.

The handler by default:

next = () => {
   res.writeHead(404);
   res.end('Not Found');
}

Properties

staticRouter.store // instance of DiskStore(als-uni-store) for managing static files
staticRouter.routes // routes