Search This Blog

2015-10-23

Creating a Node.js HTTP Client Application

This is useful when you are providing some sort of a proxy functionality or even some sort of web scraping type functionality where you are going to go out and grab web pages, and then attempt to process the HTML page, and get some information from it.

we are actually going to be using an external 'request' module.

Step 1: Run "npm init"

Step 2 : Insert all required parameter in 'npm init' and create the start page 'index.js'.

Step 3: In package.json , add the dependencies as below and run 'npm install'

"dependencies": {
"request": "*"
}


Package.json:

{
  "name": "demohttpclientapp",
  "version": "1.0.0",
  "description": "create a http client application",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "MRB",
  "license": "ISC",

  "dependencies": {
  "request": "*"
   }
}


Step 4:Write below code in your index.js

var request = require('request');
request('http://www.google.com', function(error, response, body){
if (!error && response.statusCode == 200){
console.log(body);
}
if (error){
console.log(error);
}
});

Step 5: Run the application by 'node index.js' and see the responses in console.

No comments: