A Node, Express, MongoDB Backend Stack is chosen for our Vue Shop. Other options are available of course, but this is the choice made. We will now work out how to set up this stack and how to make it work with ease using several tools.
Stack Choice
As Chris at Scotch.io mentioned this choice does not need to be your choice, but it is a great option and an option for us to learn. You can use Laravel PHP with MySQL and VueJS instead of the Node, Express MongoDB Backend Stack we have chosen. But that would be for another tutorial. Let’s stick to this and use these tools shall we?
Node
NodeJS is a JavaScript Engine that can be run the server to run JavaScript based frameworks. It is well known for its Node Package Manager which many developers use to work with CSS, SASS, LESS and JavaScript more efficiently. We will be using Node for both, as a tool and as a server runtime engine.
Express
Express is a routing framework for Node. Express calls itself a fast minimalist web framework for Node.js. Routing is one of its features. And in our case we are talking about routing of data from the frontend to the backend, passing data with GET, POST, UPDATE and so on. This is not the same as the Vue Router mentioned earlier used for frontend routing. It is more like Eloquent in Laravel. An example would be
app.get('/', function (req, res) { res.send('Hello World!') })
MongoDB
MongoDB is a flat JSON based database. MongoDB uses JSON-like documents with schemas. To quote another knowledgeable DBA on SO:
“MongoDB is not a key/value store, it’s quite a bit more. It’s definitely not a RDBMS either. I haven’t used MongoDB in production, but I have used it a little building a test app and it is a very cool piece of software. It seems to be very performant and either has, or will have soon, fault tolerance and auto-sharding (aka it will scale).
I think Mongo might be the closest thing to a RDBMS replacement that I’ve seen so far. It won’t work for all data sets and access patterns, but it’s built for your typical CRUD stuff. Storing what is essentially a huge hash, and being able to select on any of those keys, is what most people use a relational database for.
If your DB is 3NF and you don’t do any joins (you’re just selecting a bunch of tables and putting all the objects together, AKA what most people do in a web app), MongoDB would probably kick ass for you.”
Tools
To facilitate this backend API choice we will use the following tools:
- Body Parser: Middleware to expose body of request via
req.body
- Morgan: Logger middleware
- Mongoose: Utility library to enhance interaction with Mongo DB.
Body Parser
The Body Parser is middleware to expose body of request via req.body
. What does this mean? Well there is a great explanation on SO on this:
“To handle HTTP POST request in Express.js version 4 and above, you need to install middleware module called body-parser. The middleware was a part of Express.js earlier but now you have to install it separately. This body-parser module parses the JSON, buffer, string and URL encoded data submitted using HTTP POST request.”
As middleware it parses JSON buffer, string and url encoded data which you submit using POST request.
Morgan
Morgan is an HTTP request logger middleware for node.js
Mongoose
Mongoose is great for developers like us as Moka mentioned on SO:
who come from SQL database types feel very uncomfortable working with dynamic collections that have no structure defined. So Schemas in the first place helps with that. Additionally, it implements validation and other neat features in order to make sure your schema is consistent when inserting/updating/finding documents from collections.
So it helps with validation of data sent to the MongoDB database. Mongoose implements schemas for structuring the data and making it consistent when performing CRUD actions.