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

vapr-static

v0.2.1

Published

A Vapr plugin that provides a basic static file server

Downloads

2

Readme

vapr-static Build Status

Installation

npm install --save vapr
npm install --save vapr-static

Usage

This is a plugin that provides a basic static file server.

const staticFileServer = require('vapr-static');
const app = require('vapr')();
const route = app.notFound();

route.use(staticFileServer({ root: '/path/to/static/folder' }));

It's as simple as that!

This plugin also exports a function called getFilename() which returns the fully resolved filename associated with a request, typically for logging purposes. If a valid filename could not be determined (e.g., because the request was invalid), it returns null.

const { getFilename } = require('vapr-static');

route.use((req) => (res) => {
  if (req.method === 'GET' && res.code === 404) {
    console.error(`The requested file does not exist: ${getFilename(req)}`);
  }
});

Under the hood, this plugin uses vapr-conditionals to provide ETags, so you shouldn't use that plugin in combination with this one, and you shouldn't provide your own ETag implementation either. However, you may want to utilize vapr-caching and/or vapr-compress to improve the performance of serving static files.

Options

options.root

This is the only required option. It's the (string) filesystem path of the directory from which to serve static files.

options.dotfiles = false

By default, for security purposes, this plugin will not serve files that have a path segment beginning with a dot ("."). You can disable that behavior by setting this option to true.

options.noEtags = false

By default, ETags will automatically be generated to support conditional requests for static files (improving the performance of the server). However, you can disable ETags by setting this option to true.

options.broker = null

This option allows you to provide a "broker" function, which allows you to perform custom logic on incoming requests, before the filesystem is accessed.

The broker function takes two arguments. The first argument is a decoded (not containing percent-encodings) version of req.target.pathname. The second argument is the req object itself.

The broker function must return a filesystem path intended to replace the path given as the first argument. Alternatively, the broker can throw any valid vapr response.

Below are some examples of things you can do with a broker.

Serve index.html when "/" is requested

route.use(staticFileServer({
  root: '/path/to/folder',
  broker: (pathname) => {
    if (pathname === '/') return '/index.html';
    return pathname;
  },
}));

Add ".html" extension when a file extension is missing

const { extname } = require('path');

route.use(staticFileServer({
  root: '/path/to/folder',
  broker: (pathname) => {
    if (!extname(pathname)) return pathname + '.html';
    return pathname;
  },
}));

Redirect requests with trailing slashes

route.use(staticFileServer({
  root: '/path/to/folder',
  broker: (pathname, req) => {
    if (pathname !== '/' && pathname.endsWith('/')) {
      throw [308, { location: req.target.pathname.slice(0, -1) }];
    }
    return pathname;
  },
}));

Require all requests to be prefixed with "/static/"

route.use(staticFileServer({
  root: '/path/to/folder',
  broker: (pathname) => {
    if (!pathname.startsWith('/static/')) throw 404;
    return pathname.slice(7);
  },
}));