NodeJS/MongoDB REST-API with token authentication and multidb support
This is an example how to create a flexible rest api with nodejs + mongodb, permission handling, multidb support (+ caching connections), documented endpoints, test cases and using test/build-runners like travis-ci
- install nodejs
sudo apt-get updatesudo apt-get install -y gcc g++ makewget http://nodejs.org/dist/node-latest.tar.gztar -xzvf node-latest.tar.gzcd [CREATED NODE DIR]./configuremakesudo make installcurl https://www.npmjs.org/install.sh | sh- if curl is missing run
apt-get install curl
- install mongodb (check if version >2.6.x)
- open http://docs.mongodb.org/manual/tutorial/install-mongodb-on-debian/
- Follow the installation instructions (Point 1 - 4)
- clone repo & go in directory
- run
npm installto install all dependencies - run
npm install -g grunt-cli - set configs in ./config and run following commands (optional)
- run
grunt exec:install
After you have configured your mongo and installed the system the first time you only need grunt reinstall to install demodata and grunt to start server (hopefully). Your system admin credentials:
- login: test
- pw: 1234
- after reboot of your VM/System run
grunt exec:killDBgrunt exec:startDBgrunt
- current documentation open: docs/index.html
- generate documentation with
grunt exec:doc
- run
grunt tests - there should be a testcase for each endpoint (success and failure cases)
- index.js -> init server and workers (clustering)
- appServer.js -> express server, request handling, middleware for authentication
- config -> db, app, require, logging configs
- endpoints -> rest endpoints
- models -> db models (schema and model)
- middleware -> custom express middleware for authentication and multidb handling, store selection
- util -> handler and other tools
- tests -> testcases
- model and endpoint file (if there are endpoints for a model -> model and endpoint must have the same name)
- on startup all models are loaded and for all models the entpoint file
- model registers the mongoose model with its schema
- Versioning
- MultiDB support: '/api/:version/:database/:classname/id/:id/:action' -> for each client an own db
- endpoint file returns an object with keys for each method (post, get, put, delete) as values another object with keys '', 'object', '{actionname}'. '' stands for request without objectid like '/api/user'. 'object' for a object call like '/api/v1/restAPI/user/:id' where the object is placed automatically on req.object. '{actionname}' could be used if the default methods are not enough for any purpose like GET '/api/v1/restAPI/user/id/:id/rating' or GET '/api/v1/restAPI/user/activeOnes'.
- additional systemdb for managing/monitoring purposes (or if you need a backoffice for other dbs)
- a model returns mongoose schema, mongo model, and optionally systemdb flag (systemdb: true -> this model is only für systemdb, systemdb: false -> only for the other dbs, not set -> for otherdbs and systemdb)