Node.js, Express, and the MEAN/MERN tutorial

So previously I followed the tutorial I found at mongoDB’s webpage on how to how to create a MEAN and a MERN stack app. I read through the first parts on creating the server app and made the app work with only a few issues. The main issue being that the tutorial use mock data and requests you to generate a new mock data link. The method they suggest seems to be outdated though.

The tutorial itself also seems a bit outdated. I wanted to use the tutorial as a basis for my ‘creating a blog project’, from now on The Blog2 project. I quickly ran into issues with outdated versions, since both node, express and the mongoDB driver have new versions that work slightly differently. A further complication came from the fact that the MEAN tutorial project used PUG and Angular with typescript that needs to be compiled. So copying from the project became really complicated, without a deeper understanding of Express.

Therefore I decided it would be best to start learning Express fundamentals. Express has a generator to start off a project, but I thought it best to try and create the simplest possible version of a RESTful server app. Express use PUG formerly Jade as a default view engine and I began discovering what PUG is about. After some research it seems to me though that it’s too much of a hassle to learn PUG. The MEAN tutorial from mongoDB simply used it to serve a starting page for Angular, but even then it seems there are smarter approaches like simply have a public access folder for the angular entry point.

I created a simple app with two simple features:

A post route to /addDoc, with which to add documents to the collection. I used a CURL command to test the functionality:

echo ‘docs=[{“string”:”test”},{“string”:”test2″}]’ | curl -d @- localhost:3000/addDoc

The command pipes the string output from the echo command into the curl command. With a get request you can use visible parameters, like localhost/addDoc?param1=test. With a post request the parameters are hidden. Here I use curl to add the parameter ‘docs’ with a value of a JSON object to send the request.

The second route is a get request to /ld (latest documents). This route has visible the visible parameters amount and skip. So localhost:3000/ld?amount=3&skip=2 will skip the two latest documents added but get the 3 next and return the contents as a JSON object.

One particular issue i bumped into which took some troubleshooting was the fact that in the new versions of express (or node?) it is necessary to import and use a body-parser to url encode particularly the passwords sent to the MongoDB server. Otherwise special characters in the password will get escaped resulting in an authentication error.

I’m pleased to get the app working. Now that I know how to setup basic interaction with the mongoDB server, I have decided to move on to start learning Angular. This will give me a better overview of what features i need to implement on the Node.js Express app.