node js tutorial - how to create simple server - Programming Geek Programming Geek: node js tutorial - how to create simple server


all you want in technology

Post Top Ad

Post Top Ad

Tuesday, 21 November 2017

node js tutorial - how to create simple server

node js tutorial - how to create simple server - torok-technology
node js tutorial - how to create simple server - torok-technology

Hello everybody, today I am gonna explain the first lesson in our tutorial of node js server-side programming based on javascript programming language, so now I am gonna show you how to create a simple server with node js and we will learn and know ho node js works, how to install node js and some basics about the HTTP built-in module, so lets go starting !.

First of all you have to install node js environment before you start to code your simple application so to do that we will jump to this link and download node.js, when you open this link you will find two versions the first one is recommended version and the second is the latest version, so I recommend to you download the recommended version to get installing without any problems, and before you click download you should choose the system operator such as windows or make or another, so make these steps and come back.

after you had downloaded the node js version this time is to install the node js environment so you should follow the few following steps to setup it successfully:

1- click on node package which you installed and click next to start installing
2- then choose I accept the terms option to start installing
3- the installing process finished!

to test the node js is working good in your computer, open the cmd "the command prompt" fro your system and try to write node -v that refers to node js version if the version of node appeared in cmd sot it works, and you will move to the next step that chooses the text editor or ide to start code the first program with node js, I will recommend to you some of the good text editors are working fine with node js such as :

1- Webstorm IDE

2- Sublime text editor

3- Php storm

4- Atom text editor

I am using the Webstorm IDE because it's amazing and works good with javascript and help you in writing the code, so let us get starting in programming our first simple node js application and say hello world.

Firstly, you will create a javascript file with the ".js" extension so the file must look like this

after creating the file let us explain what you will do and how to create a server:

in web programming there is a thing called HTTP, HTTP is a shortcut for this sentence: "hypertext transfer protocol" so it is express on the transferring process of the data and files between the browser and the server, so if you want to build a web site with any programming language you have to build a server with calling the http module to start to transfer the data and files, node js represents to you an http built-in module to call it in your project, also node js call anything such as modules or another javascript file with this word "require" so the calling http module will be like this:

const http = require("http");

after requiring HTTP module you have to set the configuration of this module and start creating the server:

node js represents to us a function called "createServer()" to start creating the server, and inside this function, there is a function takes two parameters the first called request, and the second called response, so now I will explain these parameters:

request : this a built-in object on node js it refers to the requires that had been sent to the server to show a specific page and specific information, for example: if we want to open an about us page of a specific website, so we write something like this: http://thesitename/aboutus, this link that we required the server consider it as a request to show a specific mage and content from the server.

response: this is referred to the responding of the server for our request, and this response carries the page and the content that we required from the server.

and after creating the server we have to specify the port that will listen to our application so our createServer function will look like this:

const http = require("http");

http.createServer(function (request, response) {

after that, we can make a thing to check if our application is work and say hello world with using the console .log function to test our application on the console.

so the code will be:

const http = require("http");

http.createServer(function (request, response) {
 console.log("hello world");

and to make this code work you have to open your cmd and enter to the folder which the hello.js file existed in and write "node" word then write the name of your js file with its extension like this:

e:/ node hello.js

you will get:

hello world

now we have finished this lesson and created our first application, so wait for the next lesson, and goodbye.

No comments:

Post a Comment

Post Top Ad