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

mpm.express.rest

v0.0.6

Published

rest framework for express

Downloads

37

Readme

mpm.express.rest

NPM

Rest Framework for Express

mpm.express.rest helps developpers write restfull controllers for expressjs. With mpm.express.rest, developpers no longer needs to write glue code to link the database to express routes.

author: mparaiso [email protected] licence: LGPL version:0.0.5

###REQUIREMENTS

nodejs , npm , expressjs

###Database support through builtin adapters

  • Mongoose ( mongoose package required)
  • MongoDB ( mongodb package required)
  • Javascript Arrays

###INSTALLATION

npm install -g mpm.express.rest

###USAGE

####BASIC USAGE

######Exemple with MongoDB

Create a express app with a json parser


	var app,express;
	express=require('express');
	app=express();
	app.use(express.json());

Create a mongodb connection , requires a mongodb database ( on localhost here )


	var MongoClient,db,collection;
	MongoClient=require('mongodb').MongoClient;
	MongoClient.connect("mongodb://localhost:27017/database",function(err,db){
		//collection will be needed for the MongoDB adapter
		collection = db.collection("documents");
		done();
	});

Create the restfull controller and the adapter for MongoDB


	var done,controller,adapter,rest,http;
	
	http = require('http');
	rest = require('mpm.express.rest');
	
	done = function(){
		/**
		 * pass the express app into the constructor
		 * you can allow or forbid some verbs by passing an option object 
		 * with a allows object : 
		 * @example :
		 * new rest.Controller(app,{allow:['list','get','post','put','delete']});
		 * if no allows option , all verbs and methods are allowed. if you want
		 * a readonly restfull controller : 
		 * new rest.Controller(app,{allow:['list','get']});
		 */
		controller = new rest.Controller(app);
		// set the adapter , the MongoDBAdapter needs a MongoDB Collection object
		controller.setAdapter(new rest.adapter.MongoDBAdapter(collection))
		// call controller.handle to create all the routes needs for our rest api
		http.createServer(controller.handle()).listen(3000);
	}

The following routes have been created

GET    / 	  list resources,query parameters can be passed to the db adapter for filtering,ect...
GET    /:id   read
POST   /      create
PUT    /:id   update a resource
DELETE /:id   delete

or pass the express app into another express app


	//main express app
	var mainapp = require('./mainapp');

	done = function(){
		//pass the express app into the constructor
		controller = new rest.Controller(app);
		// set the adapter , the MongoDBAdapter needs a MongoDB Collection object
		controller.setAdapter(new rest.adapter.MongoDBAdapter(collection))
		// call controller.handle to create all the routes needed for our rest api
		mainapp.use("/api/documents/",controller.handler)
		http.createServer(mainapp).listen(3000);
	}

The following routes have been created

GET    /api/documents
GET    /api/documents/:id
POST   /api/documents/
PUT    /api/documents/:id
DELETE /api/documents/:id

And Voila! You can of course create multiple rest controllers with the same or different DB adapters.

Using Mongoose

	var app,express,http,mongoose,Schema,
	Model,controller,adapter,rest;

	http = require('http');
	rest = require('mpm.express.rest');
	mongoose=require('mongoose');
	express=require('express');

	app=express();
	app.use(express.json());
	mongoose.connect("mongodb://localhost/db");
	Schema = mongoose.Schema({message:String});
	Model = mongoose.model('Model',Schema);

	controller = new rest.Controller(express());
	controller.setAdapter(new rest.adapter.MongooseAdapter(Model,'model'));

	app.use('/api/model',controller.handle());

	http.createServer(app).listen(3000);

Changelog

  • 0.0.5 allows option is now allow

TODO

  • [ ] implement ETAG support
  • [ ] implement resource nesting (ex :/foo/:id/bar )