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-srv

v3.0.3

Published

Simple static node.js server

Downloads

173

Readme

Node-srv

Simple fast and static node.js server

Install

$ npm install -g node-srv

Usage

# Start server on port 8000 in current dir
$ node-srv

# Start server on port 8000 in parent dir
$ node-srv ..

# Start server on port 8001 writing logs to *./nodeserver.log* file
$ node-srv --port 8001 --logs ./nodeserver.log

API usage

new Server(options, routes, handlers, exitCallback);

// Require module
var server = require('node-srv');

// Start server
var srv = new Server({
    port: 5000,
    root: '../www/',
    logs: true
});

// Update server port (automatically restert server with new port)
srv.options.port = 5001;

// Stop server
srv.stop();

Options

  • -p, --port [number], port — Port the server is started on (default 8000, or env PORT)
  • -h, --host [host], host — Host or ip address on which the server will work (any host 0.0.0.0 by default)
  • -i, --index [file], index — Sets default index file for directories. For example: for uri /test/, server open test/index.html. Default index.html
  • -l, --logs [path/boolean], logs — Write logs flag. If you specify a path, it will write to that file (if path is folder, default filename will be node-srv.log). Default false
  • -t, --timeout [ms], timeout — Requset timeout (in ms). Default 30000
  • -s, --https [boolean], https — Force create HTTPS server (only with --key and --cert options). Default false
  • --key [path], key — Path to key file for https server
  • --cert [path], cert — Path to certificate file for https server
  • --cors [hosts], cors — Enable CORS. If empty uses * for host. Default false
  • --not-found [path], notFound — Path to 404 error page. Default null
  • --help — print help
  • --version — print version

Usage as Grunt.js task

  1. Install node-srv locally
$ npm i node-srv
  1. Load task into your Gruntfile
grunt.loadTasks('node-srv');
  1. Configure as multitask
grunt.initConfig({
    srv: {
        server1: {
            port: 4001,
            '404': './404.html'
            index: 'index.htm',
            keepalive: false
        },
        server2: {
            port: 4002,
            logs: true
        },
    }
});
  1. Run task
$ grunt srv:server2

Extending server

You can extend server class.

const Server = require('node-srv');

class MyServer extends Server {
    log(string) {
        console.log(string);
    }
}

Handlers

You can add custom handlres specific path patterns (like minimatch).

Parameters way:

const Server = require('node-srv');

new Server({
    // options
    port: 8000
}, {
    // routes
    '**/*.md': 'markdown', // handler name for handlers list
    '_healthcheck': (params, resolve) => { // direct handler function
        resolve({
            body: `OK: ${params.method} ${params.uri}`, // "OK: GET /_healthcheck"
            code: 200,
            headers: {'Content-Type': 'text/plain'}
        });
    }
}, {
    markdown: (params, resolve, reject) => { // handlers key-value list
        markdown.renderFile(params.file).then( html => {
            resolve({
                body: html,
                code: 200,
                headers: {'Content-Type': 'text/html'}
            }, (error) => {
                if (error.code === 'ENOENT') {
                    reject({handler: 'notFound'});
                } else {
                    reject({error});
                }
            });
        });
    }
});

Extend way:

const Server = require('node-srv');

class MyServer extends Server {
    routes() {
        return {
            '**/*.md': 'markdown',
            '_healthcheck': (params, resolve) => {
                ... // as in parameters
            }
        };
    }
    handlers() {
        return {
            markdown: (params, resolve, reject) => {
                ... // as in parameters
            }
        }
    }
}

new MyServer();

You can return HTTP code or Promise object (and resolve HTTP code).

Default handlers:

  • file — response file
  • notFound — response error 404 page (default or optional)
  • timeout — response timeout page (by default on request timeout)
  • serverError — response error 500 page. Define error code by reject({code: 403}) and page will return that.
  • options — response for OPTIONS request method (CORS)

You can override its with any way.

Breaking changes from 2.x to 3.x

CLI options:

  • -r, --root removed. Use arguments: old node-srv --root ../web, new node-srv ../web
  • --404 renamed to --not-found
  • -k shortcut removed from --key. Use only full flag
  • -c shortcut removed from --cert. Use only full flag

Program API:

  • class arguments changed
  • handlers architecture changed