Node.js in API
What is an API?
A set of functions and procedures allowing the creation of applications that access the features or data of an operating system, application, or other service. In simple words API is a server that creates an HTTP interface for interacting with data.
It is usually served in a remote machine that dictates how an application can interact with data. Basic operations like create, read, update, delete.
What about REST?
Its a most popular API design pattern used. REST is popular due to it’s simplicity and the fact that it builds upon existing systems and features of the internet’s HTTP in order to achieve its objectives, as opposed to creating new standards, frameworks and technologies.
An API design that combines DB resources, route paths, and HTTP verbs to allow applications describe what action they are trying to perform.
It became more popular when the APIs for integrations are opened by SaaS products as offering to their clients. Easy to work with basic data models but hard to scale with complex data models and its respective requirement.
Node.js , Express and MongoDB
Node.js is of course a JavaScript, it’s async, event driven and single threaded (but it can be optimize). When kept async, Node can handle a high amount of concurrent requests. It is not recommended for CPU intensive work like data crunching, ML, big maths etc.
Express is a standard API framework for node.js. It is open source and has a huge community and support from anything that has to do with APIs in Node. Express is here to stay with version upgrades, stability and is very easy to use and implement.
MongoDB in a non relational database which works like a dream in node.js. It stores non-relational document / data that is easy to get started and also scales well. It is open source and is used vastly world wide. It supports both ORM (Object Relational Mapper) which maps the relations between data, as well as ODM (Object Document Mapper) which deals with documents. MongoDB stores data in flexible, JSON-like documents, meaning fields can vary from document to document and data structure can be changed over time.
Code Setup
We need to set up and install the required dependencies and modules for the app to work correctly. We will be also creating an API, and it’s obvious a database is needed. We are using MongoDB, a NOSQL database. It’s the optimal choice for our needs as we do not have the need for a relational database. We will be installing and running it locally for now. Visit the MongoDB installation manual to install the DB for different type of machine (linux, Mac).
Since everything is javascript, we will be writing code in ES6 coding standard. We will be also using Babel which is a JavaScript compiler that compiles code to browser readable standard. Along with Babel we are using ESLint, a code linters for managing and organizing our code base for finding and fixing problematic code patterns. Nodemon for watching the file changes if there are any changes in the file so that we don’t have to restart the server in every changes. We will be also using middleware like body-parser, cors, dotenv, morgan for node communications. Lodash as utility library, jsonwebtoken for token authorization for api requests.
Below is how the package.json should look with all the dependencies.
We have also used mongoose. Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node.js. It manages relationships between data, provides schema validation, and is used to translate between objects in code and the representation of those objects in MongoDB.
Now, let’s configure babel, eslint and nodemon for the app.
Create a .babelrc
, .eslintrc.js
and nodemon.json
file with the following configuration.
Working in the app
Now we have setup everything and all the dependency are ready and installed, let’s go ahead and create src
folder inside the directory and create a file called server.js
.
In server.js
Now that we have the server.js
, let us run below command which compiles server.js
file into browser readable format.
yarn dev
Up to this point, what we have is a skeleton for the server. Now we will create an endpoint in the server which is running on port 8081 (http://localhost:8081)
.
Now, in src/server.js
Now Let’s, restart the server and visit http://localhost:8081/api
. We should be able to see the JSON formatted data.
Now that we have setup the server with sample route. Let’s configure MongoDB into the app with the help of mongoose to do some real api calls.
Lets create a database.js
file in src folder and write some mongodb connection code.
Here, we are using useNewUrlParser
, we can change useNewUrlParser
to false, it works, but shows a Deprecation warning:
(node:11481) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true }
to MongoClient.connect.
Now that the connection is made, lets jump into real actions and create some resources for the CRUD actions.
- C — model.create(), new model()
- R — model.find(), model.findOne(), model.findById()
- U — model.update(), model.findByIdAndUpdate(), model.findOneAndUpdate()
- D — model.remove(), model.findByIdAndUpdate(), model.findOneAndRemove()
Here we will be creating a listing CRUD actions. But before we begin to create a listing resource, lets create a shared/useable CRUD functions, we will named it as crud.js
inside src
folder.
Now, we have a shared crud controller function in place. So, in the src
folder lets create a nested folder called sources
which has a folder called listing
with files listing.model.js
, listing.router.js
and listing.controller.js
. Here we will be creating some listing resources.
Note: the name attribute here is unique so passing duplicate name would throw an error.
Here, we can see that a schema is created in the listing model that takes names and description, a controller with the crud functionality, and lastly a router with the crud request. Now if we try to run the code, it will fail, obviously it will fail, why? Because the crud functions in the crud.js
are all empty with empty functions. We need to complete that first before trying any operation. In the crud.js
lets include the following code.
Now that we have the crud controller in place, All we need to do it connect it with the server. Previously we just need a dummy route in the server.js
file. Lets remove that dummy route and have the listing router in the server along with the connection with mongodb.
In server.js
Lets start the server yarn dev
.
Now that the app is ready for some testing. Lets do some api testing. For this we can use some external API development environment tools like postman or insomnia. I personally use insomnia as it is very light weight and easy to use but its up to you to choose the tool for the API.
Lets do a get request in the api pointhttp://localhost:8081/api/listing
in the insomnia app.
Similarly lets do a post request in the same api point. Note: the name should be unique.
Similarly we can also find, update and delete the record with the help of the id which has the key “_id”
. Here the id for the above post request is ‘5ce8f0b95fc20462a3fa0d0f’. So the CRUD operation will have end point like:
- Read:
http://localhost:8081/api/listing/5ce8f0b95fc20462a3fa0d0f
. This will be aget
request. - Update: the endpoint url is same as read but the request will be a
put
with some updated JSON body data.{
“name”: “this is the updated name “,
“description”: “updated description for sample”,
“status”: “complete”
} - Delete: the endpoint url here is same as read, the request will be a
delete
You can try it out and see the outcome. #happyCoding
Conclusion
This is the basic api we can set in node.js with the help of express and mongodb. There are some flaws in the code like exception handling if there is an error in the api calls but thats coding for an another day.
In the next chapter we will be doing some integration. We will be integrating the api with React along with authentication using jsonwebtoken. We will be also going through some test cases using jest. And if possible docker deployment in the cloud.
Hope you coder
enjoyed reading this as much as I enjoyed writing it. Being curious, it sure has helped me a lot to grow as an engineer.
So what do you think about this this blog post? Will it be of help to others? Do not hesitate to provide feedback and also share, like the post. Just click the clap below so that other people will see this (50 claps would be nice 😉).
I would like to thank my Mentor Ashis Rai , for being my inspiration and always keeping me posting.
Peace out ✌️
Github Link: https://github.com/ramanamatya/api-design-node