Skip to content

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

License

Notifications You must be signed in to change notification settings

traynor/node-static-auth

Repository files navigation

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published