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

express-json-api

v0.0.9

Published

The quickest way to create a RESTful api from your mongoose models using the express router and jsonapi.org standards.

Downloads

19

Readme

Express JSON API

Build Status

This is an easy to use Express.js route helper that adapts your Mongoose models to jsonapi.org API endpoints. Basically it allows you to do CRUD on your mongoose models with some simple configuration.

Note; this is still very much a work in progress, but I would love to hear your thoughts.

Install

npm install express-json-api --save

Requirements

You are also required to be running expressjs and mongoose.

npm install express --save
npm install mongoose --save

Quick Start

var express = require('express');
var expressJsonApi = require('express-json-api');
var get = expressJsonApi.controllers.get;
var getList = expressJsonApi.controllers.getList;
var patch = expressJsonApi.controllers.patch;
var post = expressJsonApi.controllers.post;
var userModel = require('../models/user'); // a reference to your mongoose models

var config = {
    routes: [
        {
            endpoint: '/users',
            model: userModel,
            limit: 20,
            id: '_id',
            methods: {
                get: get.default,
                getList: getList.default,
                patch: patch.default,
                post: post.default
            },
            search: {
                active: true,
                fields: ['first-name']
            },
            sanitize: {
                active: true
            }
        }
    ]
};

expressJsonApi.factory(app, config);

Now you can access your users by:

  • GET /users to get all users
  • POST /users to create a single user
  • GET /users/:id to get a single user
  • PATCH /users/:id to update a single user

Filters, Sort, Pagination and Search

There are a number of modifers to help return the correct data. These all follow the recommendations of jsonapi.org

  • GET /users?filter[first-name]=Elon&filter[last-name]=Musk to get all users with the first name "Elon" and the last name "Musk".
  • GET /users?sort=last-name to get all users and sort by descending last name.
  • GET /users?q=Elon to get all users with "Elon" in the first-name field.
  • GET /users?page[limit]=1&page[offset]=3 to get 1 user starting at the 4th.

You can, of course, combine all those together into one long query:

GET /users?filter[country]=Australia&sort=-last-name,first-name&page[limit]=10 to get all Australian users, sort by descending last name, then ascending first name and limit the response to 10 items per page.

TODO

  • Write more thorough documentation on sanitizers, how to override individual implementations, and how to use custom serializers.
  • Create DELETE functionality.
  • Implement:
    • Stronger jsonapi response standards (eg. { data: { type: "users" } }). See http://jsonapi.org/format/#document-resource-objects
    • jsonapi relationships and links.
    • Standardised error message.
    • Dev/debugging mode so that stack traces aren't displayed in production systems.