typorm multiple connectionsexpressjs_nodejs

How to start with nodejs and expressjs to build restful APIs

Get started with Nodejs backend with Expressjs, In this blog we are going to learn about how to start the backend server or restAPI locally. Its just a beginner blog post where we just start our server. Before reading this block check out the prerequisites below to install Node and yarn or npm package manger, after Installing them start with the blog.

Prerequisites:

follow the steps,

Setup:

STEP-1:

Now start creating a blank repository in your system and go to that repository

mkdir foldername

Cd foldername

STEP-2:

We need to create a backend initial template in this blog I will show you with Yarn,

Enter “Yarn init” and enter, it will ask the template questions, you can just click enter for all the questions, default template will be created

 Yarn init

expressjs template

then you can see the below file will be created in the directory, it is the package.json file where we can see all the packages installed information

package.json file in folder

STEP-4:

For starting the server you need to install expressjs, expressjs is a framework used for nodejs backend server

Yarn add expressjs

After installing expressjs , open your favourite code editor and open the folder, When you open the folder you can see like this.

nodejs folder structure

Now we can write your code to create the server, Now I don’t have any file to right the code, we need to create a file ,The easy method is go to package.json and check the main

check initial file

Here I have the file name as index.js , the initial point to the server to run the code is index.js, There are two options.

  1. Here you can give your own file name and create a file in the repository

2. Just create the name based on the name corresponds to main in package.json

If we go with the different name change “index.js” the file name beside main to your own File name “yourname.js” and create a file in the repository.

expressjs own initial file

STEP-5:

For the blog we will go with “index.js”, create a file index.js in your repository

expressjs default initial file

Server code with expressjs

STEP-6:

Just we need couple of lines of code to start your server, First “import express “, next “const app=express() ” creates new application for you, following make that app to list at port 3000 or any port

expressjs API start code

STEP-7:

Run the command- “node index.js”, you can see the console

node index.js

After running above command you can see the below image running at port 3000

console look for running api

That’s it your server is running at port 3000 , go to browser and enter http://localhost:3000/ , you can see like this,

check get request in browser

Here you can see your localhost is running  but there is nothing to show response from server at localhost, so we add small code

STEP-8:

Add app.get(“/”) method , here app.get() means when ever this app is called with get method with the path “/”, here “/” indicates the initial path,

add get request for the api

STEP-9:

Next stop the server and restart again node index.js, you can see “welcome to API”

expressjs welcome to api

Now our backend server is running, you could have observe that you restarted the app again after saving changes , so it is annoying to restart every time you made changes, so will setup some package which will take care of it I,e ”Nodemon”

STEP-10:

Add package nodemon to the application

Yarn add nodemon

STEP-11:

While it installing go to package.json add the “start script”

nodemon script

STEP 12:

After installing nodemon ,run yarn start in command line, you can see nodemon running automatically, you can write code if anychanges nodemon checks and restart again, ignore that warning for now

Yarn start

expressjs nodemon start

That’s it for the blog, if you have any questions write down below

Conclusion:

Here in this blog, we learnt how to Start our own backend server locally using expressjs, nodejs. If you have any doubts or questions please write to me, you can comment down below. Any suggestions to Improve from my side you can write in comments too.

please check for technology blogs here.

If you liked the blog please share with your friends.

About the author

puneethrdy

View all posts