API with Mongo, Express and Node (part 1)
APIs, Application Programming Interfaces, are essential for any web and mobile applications since they serve as communication between front-end and back-end of an application. Simply put, APIs are like the waiters in restaurants. In this scenario, the menu on the table serves as the front-end while the kitchen and the kitchen staff are the back-end.
I recently embarked on creation of a CRUD(Create Read Update Delete) application or going with the analogy, creation of my restaurant. So firstly, I created the communication protocol between the kitchen and waiter. The API will be based on express, a lightweight web framework, Mongodb, a nosql database and Node which is a JavaScript run-time environment. To it then...
Firstly, create a folder/directory that will serve as the root directory. Once created, enter the directory (lack of better words, sadly).
$ mkdir crud $ cd crud
Next, initialize the package.json file. This file will be used to set the dependencies as well as the scripts to be used to run the server file. The -y flag is used to initialize defaults
$ npm init -y
The API will require a server file for server configurations, the model file for models to be used in the database as well as routes file to set up the backend routes to be used.
$ mkdir models $ mkdir routes $ touch models/data.js $ touch routes/routes.js $ touch server.js
There are several dependencies that should be used by the API to perform its functions. Firstly, there is express which is the web framework, mongoose which is a middleware that provides a schema-based structure for the data to be stored in the mongoDb. Nodemon is to be used to run the server and automatically reload the server in case of changes with the files. Once the command is run, the dependencies should be listed on the package.json. The node_modules directory and package-lock.json file will be automatically created.
$ npm install express mongoose nodemon cors --save
SERVER.JS
On this file, first we can test out the server connection. When you run the command of nodemon server/js, the words 'Cows will fly' will be on the browser when you go to the url 'https://localhost:5000/'
Mongo DB
Mongo DB is a noSQL database. It can be used locally or can be used online using the cloud infrastructure offered by Atlas. For this blog post I will be using the local database. To install the database on Ubuntu, the following link offers straight forward instructions.
To run the database, use a separate terminal and run the following command ,'mongod'. To run the shell, the 'mongo' command.
$ mongod //then run $ mongo
There are several commands that are useful for use in mongoDB. The following link offers reference for the commands.
For purposes of the API, we will create a collection called "blogPosts". Collections are like tables when compared to SQL databases. Finally, we will insert one record for the blog post.
$ use crud $ db.createCollection("blogPosts") $ db.blogPosts.insertOne( { Title: "Miracles", Post: "Cows will fly" } )
Back to server.js ...
We will now set up the server configuration file. Firstly, there is importation of express as well as mongoose packages. The express function is then called and assigned to const 'app' so that it can be used on the file. 'cors' or 'Cross-origin resource sharing' is used to ensure that the API can be accessed by mobile applications.We then declare PORT and the MONGODB_URI variables and assign them to the port number and the URL to connect to the database. When we chain the Express use method, it allows the API to have access to the libraries that were imported. The 'express.urlencoded( {extended: true } ) and express.json()' are then used for parsing requests that have JSON payloads. Then there is mounting of the routing middle-ware using express function. Next, we connect to the database using the mongoose package. We need to test if the connection to the database has been successful and shows the message on the console. Finally, we use the listen method from express to check if there is a connection on the port declared on the file. (line 26-28)
This was for beginner, I will get to part two :) where we will look at the model and the routes to be used...