Creating your first API using Nodejs + Expressjs | No Bs Tutorials

Saksham Khurana
Devs Life
Published in
3 min readSep 30, 2019

Welcome to No BS Tutorials. Here you will get “To the point” tutorials with no BS involved.

Photo by Paul Hanaoka on Unsplash

A detailed tutorial with thourough explanation will be found on my blog.

Goal :

To create a simple backend server with Nodejs.

Before We Start :

This tutorial assumes you have basic knowledge of Nodejs & NPM as well as you have installed Node on your system. If you need to learn more about Nodejs, I will recommend you going through the Nodejs tutorial on Tutorialspoint once.

With intro out, lets create a folder in our system inside which we will start working.

Getting Started :

Creating a simple server

  • Open your cmd inside your project directory & run
npm init
  • Again on cmd, run the following code to install Express dependency.
npm i express
  • After both the steps are done, create a new file hello.js & paste following code
  • Now back to cmd & run
node hello.js
  • Fire up your browser & open
you will be greeted with this

http://localhost:8071/yeah

which means our api is working 🎉

Using Query Parameters

Sometimes need to pass some value to our backend to perform operations on it.

  • Using the code below make changes to your previous file
  • Open the browser and enter url

http://localhost:8071/yeah?name=saksham

  • How about using multiple queries in same url ?
  • Again open browser & enter

http://localhost:8071/yeah?name=saksham&age=24

Using Path Parameters

Sometimes rather than just query parameters we need to pass a dynamic value in our url itself. Example -

http://localhost:8071/yeah/saksham/details

http://localhost:8071/yeah/mahomes/details
  • Update your code to

open — http://localhost:8071/yeah/saksham/details

Till now we are only working with the GET type of requests that is easily done with any kind of browser available. But there are also many other kind of requests like POST, DELETE, PUT etc.

Body Parameters (POST)

Sometimes we need to gather parameters that must be in a secure form (eg password).

For POST requests we need a client to test the same since browsers cannot send a POST requests normally. Download POSTMAN for the same.

  • Let’s start with installing body-parser dependency & use the following code
  • Open POSTMAN and follow the below screenshot to select POST, enter URL & entering PARAMS

Conclusion :

This is a basic tutorial to help you start with Express server. You can implement these things to create a basic server & you can easily get started with anything.

I will be adding more tutorials for express server like connecting to database, storing files, implementing authentication & more shortly.

Wrap Up :

Once again if you wish to know in more detail about this entire tutorial visit my blog.

Thanks a lot for tuning in today, for more tutorials follow this publication or subscribe to my blog.

--

--