Passing connection information

Since the goal of the API to connect to any database and explore it the API project cannot store connection information in config file. Instead connection info must be passed with every request.

Initially I was thinking to pass info in the body of the request, but since I have to implement GET requests also (which according to REST is not supposed to have bodies) I had two options:

  1. put connection info into request parameters (not safe)
  2. put connection info into request header

The latter solution seems to be the correct and simplest one: I used middleware in Express.js to read header and populate request’s connectionString parameter.

API Design

I found couple helpful links for anyone who wants to build API project:

http://blog.mwaysolutions.com/2014/06/05/10-best-practices-for-better-restful-api/

https://scotch.io/tutorials/keeping-api-routing-clean-using-express-routers

So at this point the API structure I think will be following:

db – get list of all databases
db/:dbname – get, put, post, delete db named as :dbname

db/:dbname/collection – get list of all collections in db named as :dbname
db/:dbname/collection/:colname – get, put, post, delete collection named :colname in db named as :dbname

db/:dbname/collection:colname/doc – get list of all documents in collection named :colname in db named as :dbname
db/:dbname/collection/:colname/doc/:docid – get, put, post, delete document with id :docid in  collection named :colname in db named as :dbname

Mongo Explorer API

There was some break since I have written anything, but I was not abandoning the project. Instead I took a class provided by MongoDB University:

https://university.mongodb.com/courses/M101N/about

I also read a book on Node.js:

 

When reading this book I felt at home first time I started learning web development – probably because I am really a back end developer.

After some consideration I came to conclusion that I need to start with API project first, so I added a new project on Github:

https://github.com/alishdev/mongo-explorer-api