GithubHelp home page GithubHelp logo

mongoose-api's Introduction

mongoose-api

Automatic REST API generation using Mongoose and Express

I had trouble finding a good solution for serving my Mongoose models as a REST API. This module can discover and serve your models automatically, or you can serve one or more of them manually.

Install

npm install mongoose-api

Usage

First, our definitions:

var express = require('express');
var mongoose = require('mongoose');
var mongooseApi = require('mongoose-api');

Let's say you define a module like so:

mongoose.connect('mongodb://localhost/test');

var TestSchema = new mongoose.Schema({
	name: String,
	description: String
});

var TestModel = mongoose.model('Test', TestSchema);

and an Express server defined like so:

app = express();
app.configure(function () {
  app.use(express.bodyParser())
  app.use(app.router)
});

Note: You must include the bodyParser and router middleware into express to handle PUT/POST requests, and to implement the served routes, respectively.

Lastly, set up the serving routes for your models, connect to the database, and start the express server.

mongooseApi.serveModels(app);

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  http.createServer(app).listen(app.get('port'), function() {
    console.log("Express server listening on port " + app.get('port'));
  });
});

Note that mongoose-api can and will automatically discover and serve routes for all your Mongoose models by default.

REST API examples

Resource index:

$ curl -X GET localhost:5000/tests
[]

Create a resource:

$ curl -X POST --data "name=Hello&description=Bonjour" localhost:5000/tests
{
  "message": "Resource created."
}

Resource index:

$ curl -X GET localhost:5000/tests
[
  {
    "name": "Hello",
    "description": "Bonjour",
    "_id": "50eb66aaedd55a0a03000001",
    "__v": 0
  }
]

Resource:

$ curl -X GET localhost:5000/tests/50eb66aaedd55a0a03000001
{
  "name": "Hello",
  "description": "Bonjour",
  "_id": "50eb66aaedd55a0a03000001",
  "__v": 0
}

Resource attribute(s):

$ curl -X GET localhost:5000/tests/50eb66aaedd55a0a03000001/description
{
  "description": "Bonjour"
}

$ curl -X GET localhost:5000/tests/50eb66aaedd55a0a03000001/name+description
{
  "name": "Hello",
  "description": "Bonjour"
}

etc...

TODO

Better docs.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.