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

node-static-auth

v1.0.6

Published

Node.js static server with Basic auth and access file logging, HTTPS support and custom error pages

Downloads

32,870

Readme

node-static-auth

Serve static files with Basic auth protection and access file logging service on top of Node.JS' native HTTP/HTTP2 server with HTTPS support.

Features:

  • static HTTP or HTTP/2server
  • HTTPS support with HTTP server listener for HTTP->HTTPS redirect
  • Basic auth protection
  • access log file with log file rotation option
  • serve your custom error pages (401, 404, 500), defaults to built-in ones (not so pretty)
  • pass native config to node-static, morgan and rotating-file-stream modules
  • disable/enable/customize features

Note about HTTP2

You must install node >= 9.x to use it, but it's only plain ole server listening on a port, so you won't get the whole nine yards, and other used modules don't really support it yet, but still.

Also, there could probably be some bugs due to its experimental status, and in combination with other modules, so they're going to be dealt with in time.

Under the hood

It bundles node-static basic-auth, morgan and rotating-file-stream modules on top of Node.js built-in HTTP, HTTP2 as well as HTTPS server. It extends static server with error pages handler with custom error pages handler option.

You can pass the same config/options for each module, as you would normally do (except for creating write stream with morgan).

Usage

  • Install:
npm i node-static-auth
  • Load module
const NodeStaticAuth = require('node-static-auth');
  • Setup config

You setup config depending on what features you want.

There are 4 main settings areas/properties in config object you must set up:

const config = {
	nodeStatic: {
		// set static server options (root, index file, custom error pages etc.)
	},
	server {
		// set web server options (ports, enable/disable http/2, https, http->https etc.)
	},
	auth: {
		// set Basic auth protection (enable/disable etc.)
	},
	logger: {
		// set logger options (enable/disable, file path, log type, log rotation etc.)
	}
}

Read the example below to see options, or go right at it here: example.

  • start the server

Pass config to server instance to start the server:

// start the server
const server = new NodeStaticAuth(config);

Example

Create HTTPS static server with access log file and Basic auth protection:

const NodeStaticAuth = require('node-static-auth');

const config = {
    // set static server
    // you can pass opts you'd usually pass to `node-static`:
    // https://www.npmjs.com/package/node-static
    nodeStatic: {
        // use path relative to project root, i.e. `process.cwd()`
        root: 'path-to-public-directory', 
        // pass the native opts for node-static here
        options: {
            indexFile: 'your-index.html'
        },
        // set your custom pages here to be served on 401, 404 and 500
        // relative to `nodeStatic.root` property, i.e. your public folder
        // NOTE: you cannot use them with HTTP2 for now, it will
        // fallback to default pages (less pretty)
        customPages: {
            forbidden: 'your-forbidden.html',
            notFound: 'your-not-found.html',
            error: 'your-error.html'
        }
    },
    // set web server options
    server: {
        port: 3001,
        // `ExperimentalWarning: The http2 module is an experimental API.`
        http2: false, // set `true` to enable, disables custom pages if set
        ssl: {
            enabled: true, // set `false` to disable
            httpListener: 3000, // set HTTP listener for HTTP->HTTPS redirect
            // enter path to certificate relative to project root
            key: 'path-to-your-privkey',
            cert: 'path-to-your-cert'
            // NOTE: browsers require TLS for HTTP2, so you've got some bogus certs
            // for localhost in the example, usable for some demo, POC etc.
        }
    },
    // set basic auth credentials
    auth: {
        enabled: true, // set `false` to disable
        name: process.env.NAME,
        pass: process.env.PASS,
        realm: process.env.REALM
    },
    // set logger file options
    logger: {
        use: true, // set `false` to disable
        // NOTE: directory will be created if it doesn't exist
        // use path relative to project root, i.e. `process.cwd()`
        filename: 'access.log',
        folder: 'path-to-logs-directory',
        // setup log rotation:
        // `https://www.npmjs.com/package/rotating-file-stream`
        // logs will be created within given folder
        logRotation: {
            use: false, // set `true` to enable
            // pass the native opts for `rfs` here
            options: {}
        },
        // pass the native opts for `morgan`:
        // https://www.npmjs.com/package/morgan
        type: 'combined',
        options: {}
    }
};

// start the server
const server = new NodeStaticAuth(config);

Or inspect it here: example.

You can configure it based on your needs, like adding log rotation, disabling logger or whatever.

NOTE:

If you omit some main settings, it will fallback to default config, similar to ones in the example above.

Also, check out test files (.spec.js) for more combinations.

Run example locally

npm i
npm start

For demo purposes, you can login with test/test, or you can setup basic auth yourself and start accordingly:

    // set basic auth credentials
    auth: {
        enabled: true, // set `false` to disable
        name: 'test' || process.env.NAME,
        pass: 'test' || process.env.PASS,
        realm: 'Restricted content' || process.env.REALM
    },

or with using your own user:

(i.e. example above):

npm i
NAME=your_name PASS=your_pass npm start

Test

npm i
gulp test

Develop

  • if you have http/2 support (node.js >=9.x), browser-sync won't work very well with http/2 so you need to test manually in that case, so run:
npm i
gulp no-bs

otherwise, just run:

npm i
gulp

TODO

  • [ ] fix lint errors;
  • [ ] feat(logger): add print to stdout option;

License

MIT