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

moonlight

v1.0.0

Published

Moonlight helps you develop Node-based RESTful web services.

Downloads

601

Readme

Moonlight

Moonlight helps you develop Node-based RESTful web services.

Features

Install

npm install moonlight

Getting started

A simple example code.

var moonlight = require('moonlight'),
    connect = require('connect'),
    http = require('http');

var app = moonlight()
    .get('/', function() {
        this.res.end('hello, world');
    })
    .get('/ping', function() {
        this.res.end('pong');
    });

app.group('/orders')
    .get('/{orderId:int}', function(orderId) {
        this.res.end('order: ' + orderId);
    })
    .post('/', function() {
        this.res.end('adding new order!');
    });

http.createServer(connect().use('/api', app)).listen(3000);

The code above does the following things.

  • It first creates a Moonlight app app.
  • Then it registers 2 methods: GET / and GET /ping.
  • It also creates a new Group, /orders.
  • Then 2 methods GET /{orderId} and POST / are registered to /orders group.
  • Lastly, it creates a connect middleware container, registers some middleware and the Moonlight app itself, and starts a HTTP server.

When you run this code, the following paths become available.

  • GET /api: will return hello, world
  • GET /api/ping: will return pong
  • GET /api/orders/1234: will return order: 1234
  • POST /api/orders: will return adding new order!
  • GET /api/api-docs: will return Swagger 1.2 resource listing JSON
  • GET /api/api-docs/orders: will return Swagger 1.2 API declaration JSON for /orders resource

Moonlight app, options, and groups

You can create a Moonlight app with some options.

var app = moonlight({ debug: false, swagger: false });

This is the default options that Moonlight instance will be using by default.

{
    "models": {},

    "swagger": {
        "apiVersion": "1.0",
        "swaggerVersion": "1.2",
        "docPath": "/api-docs",

        "info": {
            "title": "API",
            "description": "",
            "termsOfServiceUrl": "",
            "contact": "",
            "license": "",
            "licenseUrl": ""
        },

        "produces": [ "application/json" ],
        "consumes": [ "application/json" ],

        "debug": true
    },

    "debug": true
}

You can override some options like this:

var opts = {
  swagger: { info: 'My API', description: 'This is my API' }, debug: false }
};

var app = moonlight(opts);

In this case, all other options you didn't specify explicitly (like swagger.docPath or swagger.info.contact) will be the default values.

Moonlight app as Connect middleware

A Moonlight app is a fully compatible Connect middleware. You can register it using Connect#use() function.

In the first example code above, it was registered with /api path prefix, and, that makes all paths specified/used in Moonlight become relative to /api.

Path rules

When you register methods, you can use variable names as part of the path like this:

app.get('/stores/{storeId:int}', function(req, res, storeId) {});
app.delete('/orders/{orderCode:str}', function(req, res, orderCode) {});
app.get('/regions/{regionCode}/accounts/{accountId}', function(req, res, regionCode, accountId) {});

If you specify types {name:type}, passed parameter value in the callback will be in that type.

Groups

You can create topics with Moonlight#group() function. You can group a set of APIs into groups. They will be shown in the same group in API documentation, will share some features or options. Also, if you register methods to the group, their paths will be relative to the base path of the group.

API documentation

By default, all methods registered with the Groups will be shown in auto-generated API docs. But, you can still determine which groups or methods will be shown in the auto-gen docs. When it generates the API docs, Moonlight tries to infer as many information as possible so you can write less code.

References

Moonlight

moonlight([opts])

Creates a Moonlight app. For options, see here.

Moonlight#get(rule, [opts], callback)

Registers a handler callback for GET request that matches rule. You can also provide some options.

  • type: data type of this method; normally you specified the name of Model.
  • params: additional info for path parameters
  • queryParams, headerParams: info for query and header parameters
  • body: info for value passed in request BODY
  • summary
  • notes
  • produces
  • consumes
  • errors

Example

app.get('/order/{orderId:int}', {
        summary: 'Find purchase order by ID',
        desc: 'For valid response try integer IDs with value <= 5. Anything above 5 or non-integers will generate API errors',
        nickname: 'getOrderById',
        params: {
            orderId: { type: 'int', desc: 'test description' }
        },
        type: 'Order',
        errors: {
            400: 'invalid order id',
            404: 'order not found'
        }
    }, function(req, res, orderId) {
        res.end('order: ' + orderId);
    })

Moonlight#put(), Moonlight#post(), Moonlight#delete()

The same as Moonlight#get() except for the actual HTTP methods: PUT, POST, and DELETE.

Moonlight#group(basePath, options)

Creates a new Group.

Group

Group#get(), Group#put(), Group#post(), Group#delete()

The same as Moonlight#get() but their path rule is relative to the base path of the group.

Data types

You can use the following data type names in rule specifier or inside the Model definition.

  • int/int64/long: 64 bit integer
  • int32: 32 bit integer
  • str/string: string
  • bool/boolean: boolean
  • float: single point float
  • double: double point float
  • datetime, dateTime, time: date and time
  • date: date

Models

work in progress

Samples