Write your first RestAPI in Node.JS using express.

qasim ali
3 min readOct 14, 2020
Rest Apis in node.js

To become a full stack developer you have to master many things mainly frontend, backend, and DB. As a full-stack developer, I work on the MERN stack mainly. MERN stack is one of the best tech stacks used by many big tech giants today it stands for Mongo DB for Database, Express.JS (An open-source node.js framework which makes starting a server piece of cake), R for React.JS (A popular open-source frontend JS library by facebook used in various big webs like Flipkart, Facebook, Instagram, Airbnb, etc.), N for Node.JS which we are learning today.

Installations

  1. Node.JS: If you have node.js on your system you can check it by running this simple command node -v if you get an output like vXX.XX.XX you have node installed. If the node is not installed you can visit https://nodejs.org/en/download/ to install it.
  2. Create a new Directory/Folder: Create an empty folder using command or GUI.
  3. Initialize the App: To initialize this directory as a new node app use command npm init -y
  4. NPM environment to install libraries: In order to run your first API/Server, we will use Express.JS here. To install express in this app we have to run this command npm i express which will add express to the package.json file and get installed in node_modules.

Write your first API

API stands for application programming interface which means that we create an interface to our written codes and expose it on our server. The code will get complex as you further your API development like doing some DB operations and processing data then returning it. Here, We are doing a simple operation of returning a hello world string.

API Methods (CRUD)

For all APIs, you will ever write we will have to use a method. All methods have a logical meaning behind here are few methods and their usages

  1. GET (Read operation): As per the name get method API is used to request data from the backend. A get method can send data using the query string only and doesn’t have a body. (Note: Only GET Apis can be called by a browser.)
  2. PUT (Update operation): Put APIs are used to update data in the backend.
  3. POST (Create operation): Post is used to send data to the backend and used to add entries in the backend.
  4. DELETE(Delete operation): Delete is used to remove an entry from the backend.

Today we will only use the GET method to print the string hello world on the browser using our express server.

Coding the first API

Create a JS file touch index.js inside the index.js file you have to write the following code:

Here I have imported the express library which we installed earlier. After importing express I have called express() which returns app. Now, this app can be used to create APIs and start a server. So, The syntax to create APIs is app_var.<method>(“<path/url>”,<callback>). the callback function is called using two parameters generally(There are more). the request contains info like header, body, tokens, etc. and response is used to send back the response. There are many methods in response but, Here we have used res.send. In the end, we have used the listen function which is used to start a server. To start a server on some port we pass it in listen as the first argument and the second argument is a call back function which gets executed after the server has started.

Moment of truth: Hit the URL localhost:3000/hello and you will see hello world.
Now to proceed further try sending an HTML markup like "<h1>Hello world</h1>” then with an inline style. Practice this as your first class much more coming soon.

Thank You!

--

--

qasim ali
0 Followers

MERN Stack developer and trainer checkout my full profile on https://assembler123.github.io/techenthu/