Create your first Server using NodeJS.

Prathamesh More
3 min readMar 5, 2020

--

In this tutorial, we will learn to build a server using NodeJS.

What is actually the server? You may already read about servers in your textbooks or somewhere in your university syllabus. But what server actually is?

A server is a computer designed to process requests and deliver data to another computer over the internet or a local network.

That’s the simplest definition of a server. It just computer running some programs like NodeJS, Django, Ruby on Rails, Apache, etc.

There are different types of servers. I am not going to explain here. You already studied in your university syllabus and blah blah blah…

Today’s main focus is to learn by doing.

So let’s begin.

  1. You are techie, so you know how to install computer programs on Windows (at least). Download NodeJS executable from https://nodejs.org/en/download/
  2. After installation, Start writing some code.
  3. I assume that you are familiar with JavaScript basic (just basics)
  4. You can use any text editor or IDE. I will use Visual Studio Code (Download: https://code.visualstudio.com/download)
  5. Create a folder named anything you like. I named hello-world-server`on my computer.
  6. Now we have to install some packages.
  7. So we are going to use Express. Express.js, or simply Express, is a web application framework for Node.js, it is Fast, unopinionated, minimalist web framework for Node.js
  8. To install a different type of packages we are using thenpm repository. NPM is Play Store for NodeJS. You can find many different types of frameworks and libraries.
  9. Now open CMD or PowerShell and navigate folder that holding our server coding files.

10. First, run npm init to create an app. Initialization will walk you through to create apackage.json file. This file is required for running NodeJS app. Take look at apackage.json file, This file stores all metadata like the name of your app, author, entry point, etc. (Disclaimer: Don’t modify the content of this file)

11. To install the Express package, run this installation command npm install express --save . It will install all the necessary files for the Express in node_module` folder of your project.

12. Now you will see you many files in your project folder.

13. Now create the file as app.js` or server.js`. You can name anything but keep it some proper naming conventions.

app.js file

14. First, we have to import expresspackage in theapp.js file. Simple JavaScript syntax.

Let me explain the above code

app.METHOD(PATH, HANDLER) 
  • app is an instance of express.
  • METHOD is an HTTP request method, in lowercase.
  • PATH is a path on the server.
  • HANDLER is the function executed when the route is matched.

Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, and so on).

Read here more about HTTP methods https://www.restapitutorial.com/lessons/httpmethods.html

Now start-server. To start-server node app.js hit enter.

Running NodeJS server

Then load http://localhost:3000/ in a browser to see the output.

Output: Hello world

You can also render any HTML page instead of just text.

Learn to build REST APIs like PRO

Thank you!

Happy coding.

--

--

Prathamesh More
Prathamesh More

Written by Prathamesh More

Passionate, Self-taught Full Stack Web Developer that loves designing and building modern websites, applications, and APIs.

No responses yet