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

restirator

v0.0.1

Published

Turns Mongoose Models into an Express and Restify REST API.

Downloads

9

Readme

Restirator

Restirator turns Mongoose Models automatically into an Express or Restify CRUD REST API.

Kickoff your Express / Restify server by just modeling the data with mongoose and turning that into a REST API that you can use from your frontend.

Usage

First install the package (and required dependencies):

# npm install restirator body-parser mongoose --save

Then use it like this:

var restirator = require('../lib/restirator');
var express = require('express');
var bodyParser = require('body-parser');

var app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

var mongoose = require('mongoose/'),
	db = mongoose.connect('mongodb://localhost/restirator-test'),
	Schema = mongoose.Schema;

// Create a schema for our data
var MessageSchema = new Schema({
  message: String,
  number: Number,
  date: Date
});

// Use the schema to register a model with MongoDb
mongoose.model('Message', MessageSchema); 
var Message = mongoose.model('Message');

// Run our lib.
restirator(db, app);

// Serve the front-end assets (that consume the API).
app.use(express.static('public'));

module.exports = app;

if (!module.parent) {
  app.listen(port, host, function () {
    console.log("Express server listening on port %d in %s mode",
    app.address().port,
    app.settings.env
  );
});

Generated API

  • Search, GET /api/v1/<model name> with mongo search filters as query parameters
  • Create, POST to /api/v1/<model name>
  • Read, GET /api/v1/<model name>/<object id>
  • Update, PUT /api/v1/<model name>/<object id>
  • Delete, DELETE /api/v1/<model name>/<object id>

Testing restirator

There is a problem in having Express and Restify in the same Node.js process, as they both overwite features in Node's native http-object to make things easier, but end up confusing each other. What this means is you need to run the Express and Restify tests separately. To do so:

# mocha test/express_test.js 
# mocha test/restify_test.js

TODO

  • [x] Configure path
  • [x] Restify support
  • [ ] CORS -headers
  • [ ] Hapi support (if it makes sense)
  • [ ] loopback support (if it makes sense)
  • [ ] simple but solid authentication support (with mongoose plugins perhaps? or our own hooks? or register auth functions for express?)
  • [ ] linking. HATEOAS?
  • [ ] generate documentation and/or some kind of service descriptor
  • [ ] "three kinds of wrong versioning" (configurable)
  • [ ] http://jsonapi.org ?

More material

Below are interesting articles that heavily influenced the original design of restirator:

  • https://strongloop.com/strongblog/compare-express-restify-hapi-loopback/
  • http://cdnjs.com/libraries/backbone.js/tutorials/nodejs-restify-mongodb-mongoose
  • http://www.troyhunt.com/2014/02/your-api-versioning-is-wrong-which-is.html
  • https://spring.io/understanding/HATEOAS
  • http://51elliot.blogspot.fi/2013/08/testing-expressjs-rest-api-with-mocha.html

License

The library is licensed under the MIT license, see LICENSE for more details.