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

a-server

v1.1.1

Published

A dead-simple HTTP server class.

Downloads

11

Readme

License: MIT Build Status

DEPRECATED

 

Keep calm and move on.

This module is one of my earliest gigs and is mostly unjustified overhead.

 

 

a-server

A dead-simple HTTP server class. A wrapper to Node's native HTTP server, enables you to easly create and control an HTTP server.

Without a-server :-(

const http = require('http');

const server = http.createServer(function (req, res) {
    res.end('Hello beautiful world');
});

server.listen(8080, '127.0.0.1', function () {
    console.log('Listening at 127.0.0.1:8080');
});

With a-server :-)

require('a-server')().start((req, res) => {
    res.end('Hello beautiful world');
});
  • [x] A server with a dead simple API (.start(), .stop() etc).
  • [x] Zero configuration for basic usage.
  • [x] Yet, configurable.

Install

$ npm install a-server --save

Usage

const aServer = require('a-server');
const myServer = aServer(options);

myServer.start(function (req, res) {
    res.end('Hello beautiful world');
});

A Single Server

If all you need is only one server, you can:

const server = require('a-server')(options);

server.start((req, res) => {
    res.end('Hello beautiful world');
});

or even:

require('a-server')(options).start((req, res) => {
    res.end('Hello beautiful world');
});

Multiple Servers

You can create multiple servers:

const aServer = require('a-server');

const myServer1 = aServer({port:8181});
const myServer2 = aServer({port:8282});

Options

host <String>

Begin accepting connections on the specified hostname. If the hostname is null, the server will accept connections on any IPv6 address (::)
Default: process.env.IP (if exists) or '127.0.0.1'.

port <Number>

Begin accepting connections on the specified port.
Default: process.env.PORT (if exists) or 8080.

https <Object>

An HTTPS options object. Read more.
Default: false (HTTP server)

timeout <Number>

Sockets idle timeout limit in milliseconds (Node's default is 2 minutes)
Default: 5000

logs <Boolean>

Basic default logs when the server starts, when it stops and when a new app is being mounted.
Default: true

Global options vs. Own options

To setup a new a-server instance with its own individual options:

const aServer = require('a-server')();
const myServer = aServer({
    timeout: 10000
    logs: false,
});

myServer.start();

To change a-server's global defaults:

const aServer = require('a-server');

/* NOTE: This will be applied for ALL new a-server instances */
aServer.defaults.timeout = 10000;
aServer.defaults.logs    = false;

API Methods

.start(app)

Start listening for client requests.
app is a request-handler, a function for handling the request and the response.
After the server has started the .onStart() hook function gets called (see hooks below).

example:

const server = require('a-server')(options);

function app (req, res) {
    res.end('Hello beautiful world');
}

server.start(app);

.stop([callback])

Stops the server from accepting new requests. callback (function) is an optional argument. It will get called after the .onStop() hook (see hooks below).

example:

const server = require('a-server')();

function app (req, res) {
    res.end('Hello beautiful world');
}

server.start(app);

// ...

server.stop(function () {
    console.log('Goodbye!');
});

.restart([newApp])

Stops the server and starts it again with an optional new app function (a new request-handler).
Calls .stop() and .start() methods (meaning: runs the onStop and onStart hooks. Read more about hooks.

example:

const aServer = require('a-server')();

function app_1 (req, res) {
    res.end('good morning!');
}

function app_2 (req, res) {
    res.end('good evening!');
}

aServer.start(app_1);

// ...

aServer.restart(app_2);

.remount(newApp)

Replaces the server's current request-handler with a new one.
Does NOT call .stop() and.start() methods.

example:

const aServer = require('a-server')();

function app_1 (req, res) {
    res.end('good morning!');
}

function app_2 (req, res) {
    res.end('good evening!');
}

aServer.start(app_1);

// ...

aServer.remount(app_2);

.kill()

Stops the server from accepting new requests and kill its props and handlers. Calls the native server.unref().
A killed server cannot be started again.

example:

const aServer = require('a-server')();

function app (req, res) {
    res.end('Hello beautiful world');
}

aServer.start(app);

// ...

aServer.kill();

// ...

aServer.start(app_1); // --> error

Hooks

NOTE: a-server is NOT an instance of EventEmitter.

a-server has two event-like hooks:

onStart

type: <Function>
default: None.
A callback function to run after the server starts.
Gets called with the server instance as its only argument.

onStop

type: <Function>
default: None.
A callback function to run after the server stops.
Gets called with the server instance as its only argument.

 

Simply put your callback functions in those placeholders:

const myServer = require('a-server')();

myServer.onStart = function (myServer) {
    // e.g. OPEN a databse connection
};

myServer.onStop = function (myServer) {
    // e.g. CLOSE a databse connection
};

myServer.start((req, res) => {
    res.end('hello');
});

P.S.

You have access to the underlying native HTTP server using the _server property:

    const aServer = require('a-server')();

    // the native HTTP server
    console.log(server._server);