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);
}
});
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:
Post a Comment