AS a QA Engineer I’m a bit curious about the new tools and technologies in the Software Development world. Since I got a chance to test an API that is built using Spring-Boot, I wanted to learn how to make one. After doing a small research I found the Express framework on Node.js is a handy tool for me to step into API development. So, I thought of sharing a few of my tryouts as it will help anyone who is trying to work on this topic.
Step 1
First, you need to install Node.js on your computer. Then inside your working directory, you need to run the npm init command. This will create the package.json file for you.
Step 2
Then you need to install express.js on your computer using npm i express. After the framework installation, you can create an index.js file to start scripting. Inside your index.js file, the first thing you need to do is importing the express and creating the app object using express. So the first lines of code will look like this,
const express = require(‘express’);
const app = express();
Then what we need to do is assign a port for the server to listen to. You can do it by (let's take the port 5000 for now),
app.listen(5000,console.log(“server started”));
Then you can set a route with a GET request and check if everything is working. To do that the code will look like this,
app.get(‘/’,(req,res) => {res.json({msg:”this is a new msg”})})
The whole code will look like this.

Now in the terminal just type node index and in the browser try http://localhost:5000 and you will see the JSON object {msg:” this is a new msg”} displayed in the browser window.
As a best practice, it’s better if you can use the Router function in express js and separate out the routes. But for the simplicity purpose, let’s keep this simple.
As you are aware in an API we have to implement CRUD operations. So a simple get request is not enough. Let's move on to handling a POST request. Note: When you are testing the post request it will be easier if you can use Postman or SoapUI to speed up your testing process.
In the POST request implementation let’s take an example of our server getting a post request with a JSON body and we are going to grab the information and send it back as a processed response (Since we are not planning to attach a database in this article and to keep the things simple as possible).
If we try to do the above function in the same way (assume we are sending {name:” Muditha”} in the JSON body),
app.post (‘/post’,(req,res) => {res.send(`we got your post data : ${req.body.name}`)})
This post request will not send us any responce. The reason is, in express we need to use a Middle whare to parse the body data. We can do it with the use of keywords. So simply what you need to do is add the middleware to parse JSON data as follows,
app.use(express.json());
So the whole code will look like this,

So when you send a post request in Postman,

You will be getting a response as,

Cool right?
So, I hope you have a hang of it now. So far express.js is the easiest framework for me to handle CRUD operations with ease. And hope this article would give you a kick start in your microservices development in JS journey.