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

v3.0.3

Published

Mongoose + Express + Controllers + Routes + Formatters + Swagger + ReDoc + CLI

Downloads

30

Readme

Express-on is a minimal MVC framework to easily turn mongodb collections into fully fledged RESTful services supporting all http verbs.

Express is the http framwork used to handle the REST service request/response using Mongoose as the ORM layer on top of mongodb and binds the http verbs to the Query operations.

HTTP Verbs

MVC Pattern

  • models folder contains the mongoose schemas/models that defines the object modelling for the document resides in an underlying mongodb collection, it is also used for document level validation and any other mongoose specific hooks as per the Document api, more on mongoose schemas
  • routes folders containes the http routes binding for different verbs to the underlying controllers method passing the http request query params along the request. more on Express router
  • controllers folder containes the business logic needed for each operation pre submitting the operation to mongodb by simply overrding any of base Controller implementation

Controller Class

Controller class is a base class with a constructor that takes the mongoose Model as the only parameter, and implements the following methods that can be overriden to implement any custom logic, checks, etc

Controller class implementation for the methods also handles the following query parameters that might present in the request url and invokes the relevant mongoose Query method

  • find={Object} specifies the mongodb selector. If not specified, returns all documents.
  • limit=Number specifies the maximum number of documents the query will return, defaults to 10, and return all when explicitly set to 0.
  • skip=Number specifies the number of documents to skip. useful for pagination when combined with limit.
  • count=Boolean set to true to count documents matching the mongodb selector if specified, otherwise returns the total count of documents
  • distinct=String specifies a single path to get the distict values of.
  • sort=String sets the sort order, it must be a space delimited list of path names. The sort order of each path is ascending unless the path name is prefixed with - which will be treated as descending.
  • select=String specifies which document fields to include or exclude, prefixing a path with - will flag that path as excluded. When a path does not have the - prefix, it is included.

Express Router

Express routers is used to bind the http requests to the underlying controller that implements the requested operation, example

Getting Started

Install with peer dependencies:

npm install --save express

npm install --save mongoose

npm install --save express-on

Edit package.json to add start script:

  "main": "node_modules/express-on",
  "scripts": {
    "start": "node_modules/.bin/express-on"
  }

Create models\currencies.js with a basic Mongoose model:

import mongoose from 'mongoose'

let schema = new mongoose.Schema({
    _id: String,
    name: String,
    country: String
});

const Model = mongoose.model('currency', schema, 'currencies');
export default Model;

Create controllers\currencies.js with a basic controller

import { Controller } from 'express-on/controller';
import Model from '../models/currencies.js';

export class CurrenciesController extends Controller {
    constructor() {
        super(Model)
    }
}

export default CurrenciesController;

Create routers\index.js to bind http routes to controller methods

import Controller from '../controllers/currencies.js';
import { Router } from 'express';

const controller = new Controller();
const router = Router();

router.delete('/currencies/:id', controller.findByIdAndDelete.bind(controller));
router.patch('/currencies/:id', controller.findByIdAndUpdate.bind(controller));
router.put('/currencies/:id', controller.findByIdAndReplace.bind(controller));
router.get('/currencies/:id', controller.findById.bind(controller));

router.delete('/currencies/', controller.deleteMany.bind(controller));
router.patch('/currencies/', controller.updateMany.bind(controller));
router.post('/currencies/', controller.insertMany.bind(controller));
router.put('/currencies/', controller.replaceMany.bind(controller));
router.get('/currencies/', controller.find.bind(controller));

export default router;

Configure mongodb connection string:

set MONGO_URI=mongodb://localhost/test

Configure http port to listen on:

set PORT=80

Start via

node .