How to create first application in Node.js in Windows Environment

How to create first application in Node.js in Windows Environment

In this tutorial, I will tell you how you will start with your first application in Node.js. This post is very important for you when you think to start with Node.js.

You will know numerous Node.js concepts so it will be good idea for you to read again and again to make sure you have understood it properly.

In this post, we will create a real Node.js app from scrach.

In Node.js, you need to register some callback and import some required module. To import required module, we use the require directive to load Node.js modules.

Step 1: Import Required Module

In this step, first load the required module and in this example we will load http library to call Node.js library that allow us to create a web server.

var http = require('http');
Step 2: Create Server

In first step i have created http instance that represents the javascript object that will use to call http.createServer() method to launch web server.

var server = http.createServer();

We have launched the web server and save this in server variable.

  1. var http = require('http');
  2. var server = http.createServer(function(req, res) {
  3. res.writeHead(200);
  4. res.end('Your first application in Node.js!');
  5. });
  6. server.listen(8080);

You can also use same example in two way and the function to be run is the callback function.

We can first save it in a variable and pass this variable to createServer() method.

  1. var callback_var = function(req, res) {
  2. res.writeHead(200);
  3. res.end('Your first application in Node.js!');
  4. }
  5. var server = http.createServer(callback_var);
Step 3: Testing Request & Response

Create a file called main.js and put code into file and save it.

Now open your command prompt and execute the main.js to launch the server :

node main.js

Now open http://127.0.0.1:8080/ in any browser and see response :

In PHP, you get the HTML of website by following way :

  1. $response_data = file_get_contents("http://example.com");
  2. print_r($response_data);

In Node.js, you get the HTML of website by following way :


  1. var http = require('http');
  2. http.request({ hostname: 'example.com' }, function(res) {
  3. res.setEncoding('utf8');
  4. res.on('data', function(d) {
  5. console.log(d);
  6. });
  7. }).end();

Phone: (+91) 8800417876
Noida, 201301
sakarya escort akyazı escort arifiye escort erenler escort eve gelen escort ferizli escort geyve escort hendek escort otele gelen escort sapanca escort söğütlü escort taraklı escort
sakarya escort - akyazı escort - arifiye escort - erenler escort - eve gelen escort - ferizli escort - geyve escort - hendek escort - karapürçek escort - karasu escort - kaynarca escort - kocaali escort - otele gelen escort - pamukova escort - sapanca escort - söğütlü escort - taraklı escort
sapanca escort hendek escort karasu escort escort sakarya sakarya escort bayan adapazarı escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort