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

trek-static

v0.3.0

Published

Serve static files

Downloads

25

Readme

static

Serve static files middleware.

Installation

$ npm install trek-static --save

Examples

'use strict'

const fs = require('fs')
const { normalize, join } = require('path')
const Engine = require('trek-engine')
const serveStatic = require('..')

const root = normalize(__dirname)

async function start () {
  const app = new Engine()

  const faviconHandle = await serveStatic.favicon(join(root, 'favicon.ico'))
  const indexHandle = serveStatic.content('text/html', fs.readFileSync(join(root, 'index.html')))
  const serveHandle = await serveStatic.serve('/ls/', root, 1)
  const webHandle = await serveStatic.web('/web/', join(root, '..'), 1)
  const fsHandle = serveStatic.fs('/fs/', join(root, '..'), 1)
  const staticHandle = serveStatic.static('/static/', join(root, '..'), 1)

  app.use(async (ctx, next) => {
    const { req, res } = ctx
    const requestPath = req.path

    let handle
    if (requestPath === '/') {
      handle = indexHandle
    } else if (requestPath === '/favicon.ico') {
      handle = faviconHandle
    } else if (requestPath.startsWith('/ls/')) {
      handle = serveHandle
    } else if (requestPath.startsWith('/web/')) {
      handle = webHandle
    } else if (requestPath.startsWith('/fs/')) {
      handle = fsHandle
    } else if (requestPath.startsWith('/static/')) {
      handle = staticHandle
    }

    if (handle) await handle(ctx, next)
    else res.send(404)

  })

  app.on('error', (err, ctx) => {
    console.log(err)
  })

  app.run(3000)
}

start().catch(console.error)

API

  • content (type, body, options)

    Serves bytes, buffers, memory cached, on the req.path.

  • async favicon (path, options)

    Serves static favicon, favicon.ico favicon.png etc.

  • fs (relativePath, systemPath, stripSlashes)

    Serves a system directory and generates an index page which list all files.
    It will generate compressed files.

  • static (relativePath, systemPath, stripSlashes)

    Static registers a route which serves a system directory
    this doesn't generates an index page which list all files
    no compression is used also, for these features look at fs function.

  • async serve (relativePath, systemPath, stripSlashes)

    Serves a directory as web resource
    It uses gzip compression (compression on each request, no file cache).

  • async web (relativePath, systemPath, stripSlashes)

    Serves a system directory, if index.html exists and request uri
    is / then display the index.html's contents, else not index.html exists then
    generates an index page which list all files.
    it will not generate compressed files.

  • options

    {
      // Relative path for request
      relativePath: '/',
    
      // Path to the root directory to serve files from.
      root: '',
    
      // StripSlashes indicates how many leading slashes must be stripped
      // from requested path before searching requested file in the root folder
      stripSlashes: 0,
    
      // List of index file names to try opening during directory access.
      indexNames: [],
    
      // Index pages for directories without files matching IndexNames are automatically generated if set.
      generateIndexPages: false,
    
      // Ignore files
      ignoredFiles: ['.DS_Store', '.git/'],
    
      // Path rewriting function.
      pathRewrite: undefined,
    
      // Enables byte range requests if set to true.
      acceptByteRange: true,
    
      // Transparently compresses responses if set to true.
      compress: true,
    
      // Cache control max age (ms) for the files, defaults to 8.76 hours = 31536000 ms.
      cacheControl: undefined,
      maxAge: 60 * 60 * 1000 * 8.76,
    
      // Expires: 358 days
      expires:  (365 - 7) * 24 * 60 * 60 * 1000,
    
      // Manipulate the HTTP Vary header
      vary: 'accept-encoding',
    
      // Etag options
      etag: { weak: true },
    
      // lru-cache options, pass to lru-cache lib
      lruCacheOptions:; 100
    }

Badges

Build Status codecov


fundon.me  ·  GitHub @fundon  ·  Twitter @_fundon