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

@sugo/serve-static

v2.1.0

Published

Static file serving handler

Downloads

6

Readme

@sugo/serve-static

Handler for serving static files. Router agnostic. Recommended use with @sugo/router

serveStatic

Returns a middleware that reads and sends files.

Options

  • @param {\string} dir: The directory in which the files are stored.
  • @param {\string} baseUrl: The base url on the request to indicate that a static file is requested. If baseUrl is /base and the request url is /base/bar/foo.txt, then the file to be search will correspond to the path /bar/foo.txt.
  • @param {\string} notFoundRedirectPath: The filepath to be served if the requested file was not found. Can be used to render a 404 page. If not set, then it will throw an error when the file is not found. It this path is not found, it will also throw an error.

Requirements

node version >= 8.12.0

How to install

npm install --save @sugo/serve-static

Requesting a directory

If the user request a directory instead of a file, the middleware will search for a index.html in that directory.

Example - Server

import { createServer } from '@sugo/server';
import Router from '@sugo/router';
import { resolve } from 'path';
import serveStatic from '@sugo/serve-static';

const server = createServer((req: SuGoRequest, res: SuGoResponse) => router.handle(req, res)).useMiddleware(
  serveStatic({
    dir: resolve(__dirname, './public'),
  }),
);

Example - With routes

import { createServer } from '@sugo/server';
import Router from '@sugo/router';
import { resolve } from 'path';
import serveStatic from '@sugo/serve-static';

const router = new Router();
router.get(
  '/public/(.*)',
  serveStatic({
    baseUrl: '/public',
    dir: resolve(__dirname, './public'),
    notFoundRedirectPath: '404.html',
  }),
);
const server = createServer((req: SuGoRequest, res: SuGoResponse) => router.handle(req, res));

Example - NodeHttp

import * as http from 'http';
import serveStatic from '@sugo/serve-static';

const middleware = serveStatic({
  dir: resolve(__dirname, './public'),
});
const server = http.createServer((req, res) => middleware(req, res));

**Example - Hosting a Single Page Application (ReactJS, Angular, VueJS) **

import { createServer } from '@sugo/server';
import Router from '@sugo/router';
import { resolve } from 'path';
import serveStatic from '@sugo/serve-static';

const router = new Router();
router.get(
  '/webapp/(.*)',
  serveStatic({
    baseUrl: '/webapp',
    dir: resolve(__dirname, './webapp'),
    notFoundRedirectPath: 'index.html',
  }),
);
const server = createServer((req: SuGoRequest, res: SuGoResponse) => router.handle(req, res));