NodeJS API + crypto + token authentication + multidbsupport
- install nodejs (
sudo apt-get update
sudo apt-get install -y gcc g++ make
wget http://nodejs.org/dist/node-latest.tar.gz
tar -xzvf node-latest.tar.gz
cd [CREATED NODE DIR]
./configure
make
sudo make install
curl https://www.npmjs.org/install.sh | sh
- install mongodb (check if version >2.6.x)
apt-get install mongodb
- clone repo & go in directory
- run npm install to install all dependencies (run
npm install -g grunt-cli
) afterwards - set configs in ./config and run following commands as sudo
- run
grunt exec:killDB
- run
grunt exec:startFirstDB
- run
grunt exec:createMongoUsers
// creates mongo users to restrict access - run
grunt exec:killDB
// stops all mongo services - run
grunt exec:startDB
// start mongo session with authentication - run
grunt initSystem -username [sysusername] -password [syspassword] -email [sysemail]
// creates systemdb with sysuser - run
grunt reinstall
// creates/recreates test endpoint db 'test' with test data (user, admin, ...) accepts '-target' flag to allow to create a new db with testdata - run
grunt
orgrunt start
to start nodejs server
After you have configured your mongo (you only need grunt reinstall and grunt)
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)
- For each model an endpoint file with the same name must exist
- 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'
- 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)
- grunt task for db backup (export/import)