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

gosling

v3.0.0

Published

Gosling is a simple es6 node server, similar to express.

Downloads

9

Readme

Gosling

Travis npm npm

Gosling is simple, plugin ready es6 node server similar to express.

Basic Use

import Gosling from 'gosling';

const app = new Gosling(1337);
app.listen();

app.use('/', () => (request, response, next) => {
    response.write('Hello World!');
    next();
});

HTTPS

HTTPS can be used out of the box with Gosling. Pass a node https options object to the constructor.

Note: HTTPS Options should either be the first or second argument, if second we assume Port is the first.

import { readFileSync } from 'fs';
import Gosling from 'gosling';

const httpsOptions = {
  key: readFileSync('./ssl/key.pem'),
  cert: readFileSync('./ssl/cert.pem')
};

const app = new Gosling(1337, httpsOptions);

app.listen();

Router

Gosling ships with a router bundled, it can be accessed by importing separately

import Gosling, { Router } from 'gosling';

const app = new Gosling(1337);
const router = new Router();

app.listen();

router.use('/', () => (req, res, next) => {});
app.use('/api', router);

When assigning a path to router that router is then scoped to only process request that match the prefix.

Routers are completely recursive and can be nested deeply.

import { Router } from 'gosling';

const router = new Router();
const subrouter = new Router();

subrouter.get(/\/post\/[a-z0-9]+$/, bobsLawBlog);
router.use('/api/blog', subrouter);

export router;

In Depth Use

Gosling's constructor takes the following optional arguments:

  • port (Number);
  • HTTPS Options (Object);
  • middleware (as Request Objects);

Full API

Once instantiated Gosling offers the following methods:

  • listen

    • Starts server.
    • Arguments:
      • port (Number) - throws error if port is already assigned
      • callback (Function)
  • close

    • Stops server
    • Arguments:
      • callback (Function)
  • port

    • Sets port if not set through constructor
    • Throws error if port is already assigned.
    • Getter / Setter
  • use (Universal request)

    • Universal Request (runs for all requests)
    • Arguments:
      • Path [optional] String or RegExp
      • Thunk
  • get

    • Only GET Requests
    • Arguments:
      • Path [optional] String or RegExp
      • Thunk
  • post

    • Only POST Requests
    • Arguments:
      • Path [optional] String or RegExp
      • Thunk
  • put

    • Only PUT Requests
    • Arguments:
      • Path [optional] String or RegExp
      • Thunk
  • delete

    • Only DELETE Requests
    • Arguments:
      • Path [optional] String or RegExp
      • Thunk

Methods API in depth

The methods (get, post, put, delete, use) API takes two arguments and creates Request Objects… - Path (as String or RegExp) [optional] - Thunk (function returning function) [required] - Note: All method calls are chainable app.use().get().post() is valid.

Request Objects

Request Objects are the heart of Gosling's speed and simplicity. They can be hand coded or passed through the method API reducer.

// Request Object
{
    path: '/' /* String or RegExp */
    method: /GET/i /* RegExp only */
    thunk() {
        /* some code */
        return (request, response, next) => {
            /* response or request modifications */
            next();
        }
    }
}

Sample Usage

You can use the simple API to produce Request Objects by:

app.use('<Path>', () => (req, res, next) => {});
app.get('<Path>', () => (req, res, next) => {});
app.post('<Path>', () => (req, res, next) => {});
app.put('<Path>', () => (req, res, next) => {});
app.delete('<Path>', () => (req, res, next) => {});

Notes

  • the methods API does the work of Method checking, so no need to pass that.
  • the use method will try to run on every request.
  • if no path is passed in, the thunk is processed on all matching request methods.
    • app.use(() => (req, res, next) => {}) will be run on all requests.
  • All method calls are chainable
    • app.use(() => {}).get(() => {}).post(() => {});