{"id":20519,"date":"2018-02-12T16:15:27","date_gmt":"2018-02-12T14:15:27","guid":{"rendered":"http:\/\/www.webcodegeeks.com\/?p=20519"},"modified":"2018-02-08T15:16:23","modified_gmt":"2018-02-08T13:16:23","slug":"node-js-http-request-example","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/","title":{"rendered":"Node.js HTTP Request Example"},"content":{"rendered":"<p>In this post we take a look at how to make HTTP Requests with Node.js. This feature is key and important capability for a server side language. With Node.js being positioned as one of the best options for aggregating multiple services and high throughput due to its non blocking I\/O operations, it is an important feature to learn about. So let us build an example where we make exactly this in Node.js.<\/p>\n<h2>1. Tools &amp; Technologies<\/h2>\n<p>I have used the following toolset &amp; technologies to build the example application. The tools may be switched out with others you are more comfortable with.<\/p>\n<ol>\n<li><a href=\"https:\/\/nodejs.org\/\" target=\"_blank\" rel=\"noopener\">Node.js v6.3.0<\/a><\/li>\n<li>HTTP Module<\/li>\n<li><a href=\"https:\/\/www.npmjs.com\/package\/request\" target=\"_blank\" rel=\"noopener\">request Module v2.83.0<\/a><\/li>\n<li><a href=\"https:\/\/www.npmjs.com\/package\/json-server\" target=\"_blank\" rel=\"noopener\">json-server v0.12.1<\/a><\/li>\n<li><a href=\"https:\/\/code.visualstudio.com\/download\" target=\"_blank\" rel=\"noopener\">Visual Studio Code IDE<\/a><\/li>\n<\/ol>\n<p>[ulp id=&#8217;7Yp6ijpjjxyeaN8A&#8217;]<br \/>\n&nbsp;<br \/>\nNode.js is a JavaScript framework for running JavaScript on the server side. HTTP module, part of Node.js, allows us to handle requests from browser and issue HTTP Requests of our own to services. We will use the json-server module to mock a service API. I will go through, in a later section, with the details of setting all of this up. I have used the Visual Studio Code IDE as I feel most comfortable with it, though you are free to switch to one of many available IDEs out there.<\/p>\n<h2>2. Project Layout<\/h2>\n<p>The structure of our project would look like below:<\/p>\n<figure id=\"attachment_20759\" aria-describedby=\"caption-attachment-20759\" style=\"width: 361px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/02\/ProjectLayoutNodejsHttpProject.jpg\"><img decoding=\"async\" class=\"size-full wp-image-20759\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/02\/ProjectLayoutNodejsHttpProject.jpg\" alt=\"\" width=\"361\" height=\"394\" srcset=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/02\/ProjectLayoutNodejsHttpProject.jpg 361w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/02\/ProjectLayoutNodejsHttpProject-275x300.jpg 275w\" sizes=\"(max-width: 361px) 100vw, 361px\" \/><\/a><figcaption id=\"caption-attachment-20759\" class=\"wp-caption-text\">Project Layout<\/figcaption><\/figure>\n<p>You can easily initialize and prepare an initial project structure using the commands below at the root of the folder you want it to reside in.<\/p>\n<pre class=\"brush: bash;\">&gt; npm init\r\n<\/pre>\n<p>This command asks a few basic questions and suggests defaults for them as well. Apart from a few you can go along with the suggestions to setup initial project structure.<br \/>\nYou can create the <code>css, js<\/code> folders and the files in them manually using command line or GUI or from within the IDE. The two modules that our example depends on and their dependencies can be installed and saved using the following commands:<\/p>\n<pre class=\"brush: bash;\">&gt; npm install --save request\r\n<\/pre>\n<p>followed by<\/p>\n<pre class=\"brush: bash;\">&gt; npm install --save json-server\r\n<\/pre>\n<h2>3. Server Side Implementation with Http &amp; json-server Modules<\/h2>\n<p>We use Http to create a web server of our own. This server would listen to service requests from the browser and relaying them on to our own mock service. Apart from this it will also handle requests for resource files like JavaScript, css and Html files.<\/p>\n<h3>3.1 Http Module<\/h3>\n<p>The http module is part of Node.js and gives us the capability to create a basic web server. We include this module through a require statement and then call its <code>createServer<\/code> method. This method takes a callback to which it passes the response and request object for the incoming requests. We also specify the <code>hostname<\/code> and <code>port<\/code> to listen for requests on.<br \/>\nWe use the <code>url<\/code> module, part of Node.js, to handle incoming requests and return appropriate responses for each. The code for this resides in the file <code>index.js<\/code> at the root of our project. Our <code>index.js<\/code> file with the web server code looks like below:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>index.js<\/em><\/span><\/p>\n<pre class=\"brush: js;\">\/\/including Node.js modules\r\nvar http = require(\"http\");\r\nvar url = require(\"url\");\r\nvar fs = require(\"fs\");\r\n\r\n\/\/Url of our web server\r\nvar host = \"localhost\";\r\nvar port = 8090;\r\n\r\n\/\/Setting up our web server\r\nhttp.createServer(function(req, res){\r\n    var path = url.parse(req.url, true);\r\n    if(path.pathname.endsWith(\"html\")){\r\n        fs.readFile(\".\" + path.pathname, function(err, data){\r\n            res.writeHead(200, \"OK\", { \"Content-Type\": \"text\/html\"} );\r\n            res.write(data);\r\n            res.end();\r\n        });\r\n    } else if(path.pathname.endsWith(\"css\")){\r\n        fs.readFile(\".\" + path.pathname, function(err, data){\r\n            res.writeHead(200, \"OK\", { \"Content-Type\": \"text\/css\"});\r\n            res.write(data);\r\n            res.end();\r\n        });\r\n    } else if(path.pathname.endsWith(\"js\")){\r\n        fs.readFile(\".\" + path.pathname, function(err, data){\r\n            res.writeHead(200, \"OK\", { \"Content-Type\": \"text\/javascript\"});\r\n            res.write(data);\r\n            res.end();\r\n        });\r\n\r\n    } else if(path.pathname.endsWith(\"names\")){\r\n        switch(req.method){\r\n            case \"GET\": api.getRequest(res, path.query.team);            \r\n            break;\r\n            case \"POST\": \r\n            var data = \"\";\r\n            req.on(\"data\", function(d){\r\n                data+=d;\r\n            });\r\n            req.on(\"end\", function(){\r\n                api.postRequest(data, res);\r\n            });            \r\n            break;            \r\n            case \"DELETE\":\r\n            var delData = \"\";\r\n            req.on(\"data\", function(d){\r\n                delData += d;\r\n            });\r\n            req.on(\"end\", function(){\r\n                api.deleteRequest(res, delData);\r\n            });\r\n            break;\r\n        }\r\n    }\r\n}).listen(port, host);\r\n<\/pre>\n<p>In the Next section, we setup our mock service using the <code>json-server<\/code> module.<\/p>\n<h3>3.2 json-server Module<\/h3>\n<p>This module allows us to quickly mock a service API. The API is backed with a store of some test data in a file called <code>db.json<\/code>. Our data store is in the folder called <code>db<\/code> and its contents look as follows:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>db.json<\/em><\/span><\/p>\n<pre class=\"brush: js;\">{\r\n  \"myTeam\": [\r\n    {\r\n      \"id\": 1,\r\n      \"name\": \"Mahendra Singh Dhoni\"\r\n    },\r\n    {\r\n      \"id\": 2,\r\n      \"name\": \"Virat Kohli\"\r\n    },\r\n    {\r\n      \"id\": 3,\r\n      \"name\": \"Bhuvneshvar Kumar\"\r\n    },\r\n    {\r\n      \"id\": 4,\r\n      \"name\": \"Mohmd. Shami\"\r\n    },\r\n    {\r\n      \"id\": 5,\r\n      \"name\": \"Ishant Sharma\"\r\n    },\r\n    {\r\n      \"id\": 6,\r\n      \"name\": \"Ajinkya Rahane\"\r\n    },\r\n    {\r\n      \"id\": 7,\r\n      \"name\": \"K L Rahul\"\r\n    },\r\n    {\r\n      \"id\": 8,\r\n      \"name\": \"Yuvraj Singh\"\r\n    },\r\n    {\r\n      \"id\": 9,\r\n      \"name\": \"Harbhajan Singh\"\r\n    },\r\n    {\r\n      \"id\": 10,\r\n      \"name\": \"Kuldeep Yadav\"\r\n    },\r\n    {\r\n      \"id\": 11,\r\n      \"name\": \"Yajuvendra Chahal\"\r\n    }\r\n  ],\r\n  \"yourTeam\": [\r\n    \r\n  ]\r\n}\r\n<\/pre>\n<p>As evident from the data we will build a page showing my dream cricket team whilst allowing users to create one of their own with their favorite players.<br \/>\nThe code to setup this mock service resides in the <code>index.js<\/code> file and it looks like below:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>index.js<\/em><\/span><\/p>\n<pre class=\"brush: js;\">var jsonServer = require('json-server');\r\nvar server = jsonServer.create();\r\nvar router = jsonServer.router('.\/db\/db.json');\r\nvar middlewares = jsonServer.defaults();\r\n\r\nserver.use(middlewares);\r\nserver.use(router);\r\n\r\nserver.listen(9090, function() {\r\n  console.log('JSON Server is running');\r\n});\r\n<\/pre>\n<p>As you can notice from the snippet the mock service runs on the URL <code>http:\/\/localhost:9090<\/code>.<br \/>\nNow finally we create a file named <code>http.request.server.js<\/code> to organize the code for making service calls to our mock service. We later include it into our <code>index.js<\/code> file through a require statement. In this file we have methods to enable requests using different HTTP verbs like <code>GET, POST, DELETE<\/code> among others. The <code>http.request.server.js<\/code> file looks like below:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>http.request.server.js<\/em><\/span><\/p>\n<pre class=\"brush: js;\">var request = require(\"request\");\r\nvar host = \"http:\/\/127.0.0.1\";\r\nvar port = 9090;\r\nvar api ={\r\n    getRequest:function(res, team){\r\n        if(team){\r\n            request.get(host + \":\" + port + \"\/yourTeam\", function(error, response, body){\r\n                res.writeHead(200, \"OK\", { \"Content-Type\":\"application\/json\", \"Access-Control-Allow-Origin\" : \"http:\/\/localhost:8090\"});\r\n                res.write(body);\r\n                res.end();\r\n            });\r\n        }\r\n        else{\r\n            request.get(host + \":\" +port + \"\/myTeam\", function(error, response, body){\r\n                res.writeHead(200, \"OK\", { \"Content-Type\":\"application\/json\", \"Access-Control-Allow-Origin\" : \"http:\/\/localhost:8090\"});\r\n                res.write(body);\r\n                res.end();\r\n            });\r\n        }\r\n    },\r\n    postRequest: function (player, res){\r\n        request.post(host + \":\" + port +\"\/yourTeam\", function(error, response, body){\r\n            res.writeHead(200, \"OK\");\r\n            res.write(body);\r\n            res.end();\r\n        }).form(JSON.parse(player));\r\n    },\r\n    deleteRequest: function (res, id){\r\n        var player = JSON.parse(id);\r\n        request.del(host + \":\" + port + \"\/yourTeam\/\" + player.playerId, function(error, response, body){\r\n            res.writeHead(200, \"OK\");\r\n            res.write(body);\r\n            res.end();\r\n        });\r\n    }\r\n};\r\nmodule.exports = api;\r\n<\/pre>\n<p>We use the <code>request<\/code> module to make calls to the mock service. As you can see from the code above we have a method for each one of the HTTP verbs we will use in our example, although all HTTP verbs are supported by this mock service. We will stick to <code>GET, POST, DELETE<\/code> to keep things simple in our example.<\/p>\n<h2>4. Client Side Implementation with JavaScript, HTML &amp; CSS<\/h2>\n<p>Onwards now to build our front end and make calls to the server as a result of user actions. We start off with a standard <code>HTML<\/code> and adding markup to display two teams as ordered lists. We also add html to allow the user to add players to their own team. Each team member added by the user would also have a link to remove the player from the list. We achieve the hover effect and styling by adding css in the <code>http.request.css<\/code> file. Our final Html markup looks like below:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>index.html<\/em><\/span><\/p>\n<pre class=\"brush: html;\">  \r\n&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n&lt;head&gt;\r\n&lt;meta charset=\"utf-8\"&gt;\r\n&lt;meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"&gt;\r\n&lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"&gt;\r\n&lt;!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --&gt;\r\n&lt;title&gt;WCG -- Node.js HTTP Request Example&lt;\/title&gt;\r\n\r\n&lt;link rel=\"Stylesheet\" href=\"css\/http.request.css\"\/&gt;\r\n&lt;!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --&gt;\r\n&lt;!-- WARNING: Respond.js doesn't work if you view the page via file:\/\/ --&gt;\r\n&lt;!--[if lt IE 9]&gt;\r\n&lt;script src=\"https:\/\/oss.maxcdn.com\/html5shiv\/3.7.3\/html5shiv.min.js\"&gt;&lt;\/script&gt;\r\n&lt;script src=\"https:\/\/oss.maxcdn.com\/respond\/1.4.2\/respond.min.js\"&gt;&lt;\/script&gt;\r\n&lt;![endif]--&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;h1&gt;WCG -- Node.js HTTP Request Example&lt;\/h1&gt;\r\n&lt;div class=\"teams\"&gt;\r\n&lt;div class=\"myteam\"&gt;\r\nMy All Star Cricket 11\r\n&lt;\/div&gt;\r\n&lt;div class=\"yourteam\"&gt;\r\nYours?\r\n&lt;\/div&gt;\r\n&lt;div class=\"addplayers\"&gt;\r\n&lt;input type=\"text\" placeholder=\"Player Name\" id=\"playerName\"\/&gt;\r\n&lt;input type=\"button\" value=\"Add\" id=\"addPlayer\" \/&gt;\r\n&lt;\/div&gt;\r\n&lt;\/div&gt;\r\n&lt;script src=\"js\/http.request.js\"&gt;&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<h3>4.1 Ajax calls with JavaScript<\/h3>\n<p>We use the <code>XMLHttpRequest<\/code> object to issue calls to the server which in turn makes calls to our mock service. The code for the client side resides in <code>http.request.js<\/code> file. We load both teams at startup. Apart from this we make a call to add team players in user&#8217;s team on the click of the <code>Add<\/code> button. Similarly we make a call when the delete player link is clicked. After each call we also make request for the updated team. Our client side code now looks as follows:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>http.request.js<\/em><\/span><\/p>\n<pre class=\"brush: js;\">var myteam = document.getElementsByClassName(\"myteam\")[0];\r\nvar addPlayer = document.getElementById(\"addPlayer\");\r\nvar playerName = document.getElementById(\"playerName\");\r\nvar deleteLink = document.getElementsByClassName(\"deletelink\");\r\nvar yourteam = document.getElementsByClassName(\"yourteam\")[0];\r\nvar host = \"http:\/\/localhost\";\r\nvar port = \"8090\";\r\n\r\nfunction setupDeleteLink(){\r\n    for(var l=0; l&lt;deleteLink.length; l++){\r\n        deleteLink[l].addEventListener(\"click\", function(event){\r\n            var xmlHttp = new XMLHttpRequest();\r\n            var ctrl = event.target;\r\n            xmlHttp.onreadystatechange = function(){\r\n                if(xmlHttp.status === 200 &amp;&amp; xmlHttp.readyState === 4){\r\n                    console.log(xmlHttp.responseText);\r\n                    refreshYourTeam();\r\n                }\r\n            };\r\n            xmlHttp.open(\"DELETE\",  host +\":\" + port + \"\/names\");\r\n            xmlHttp.send(JSON.stringify({ \"playerId\": ctrl.getAttribute(\"playerId\")}));\r\n        });\r\n    }\r\n}\r\n\r\naddPlayer.addEventListener(\"click\", function(){\r\n    var player = {};\r\n    player.name = playerName.value;\r\n    var xmlHttp = new XMLHttpRequest();\r\n    xmlHttp.onreadystatechange = function(){\r\n\r\n        if(xmlHttp.status === 200 &amp;&amp; xmlHttp.readyState === 4){\r\n            console.log(xmlHttp.responseText);\r\n            refreshYourTeam();\r\n        }\r\n    \r\n    };\r\n    xmlHttp.open(\"POST\", host +\":\" + port + \"\/names\");\r\n    xmlHttp.send(JSON.stringify(player));\r\n    \r\n});\r\n\r\nfunction Initialize(){\r\n    var xmlHttp = new XMLHttpRequest();\r\n    xmlHttp.onreadystatechange = function(){\r\n\r\n        if(xmlHttp.status === 200 &amp;&amp; xmlHttp.readyState === 4){\r\n            console.log(xmlHttp.responseText);\r\n            populateMyTeam(xmlHttp.responseText);\r\n        }\r\n    \r\n    };\r\n    xmlHttp.open(\"GET\", host + \":\" + port + \"\/names\", true);    \r\n    xmlHttp.withCredentials = false;\r\n    xmlHttp.send();\r\n}\r\n\r\nfunction refreshYourTeam(){\r\n    var xmlHttp = new XMLHttpRequest();\r\n    xmlHttp.onreadystatechange = function(){\r\n\r\n        if(xmlHttp.status === 200 &amp;&amp; xmlHttp.readyState === 4){\r\n            console.log(xmlHttp.responseText);\r\n            populateYourTeam(xmlHttp.responseText);\r\n        }\r\n    \r\n    };\r\n    xmlHttp.open(\"GET\", host + \":\" + port + \"\/names?team=yourTeam\", true);\r\n    xmlHttp.send();\r\n}\r\nfunction populateYourTeam(team){\r\n    var orderedList = document.createElement(\"OL\");\r\n    var players = JSON.parse(team);\r\n\r\n    for(var i=0; i &lt; players.length; i++){\r\n        var listItem = document.createElement(\"LI\");\r\n        var delLink = document.createElement(\"SPAN\");\r\n        delLink.className = \"deletelink\";\r\n        delLink.appendChild(document.createTextNode(\"X\"));\r\n        delLink.setAttribute(\"playerId\", players[i].id);\r\n        listItem.appendChild(document.createTextNode(players[i].name));\r\n        listItem.appendChild(delLink);\r\n        orderedList.appendChild(listItem);\r\n        \r\n    }\r\n    yourteam.innerHTML = orderedList.outerHTML;\r\n\r\n    setupDeleteLink();\r\n}\r\nfunction populateMyTeam(team){\r\n    var orderedList = document.createElement(\"OL\");\r\n    var players = JSON.parse(team);\r\n\r\n    for(var i=0; i &lt; players.length; i++){\r\n        var listItem = document.createElement(\"LI\");\r\n        \r\n        listItem.appendChild(document.createTextNode(players[i].name));\r\n       \r\n        orderedList.appendChild(listItem);\r\n        \r\n    }\r\n    myteam.appendChild(orderedList);\r\n\r\n}\r\nInitialize();\r\nrefreshYourTeam();\r\n<\/pre>\n<h2>5. Running the Application<\/h2>\n<p>To run the application you need to navigate to the root of the application and run the following command:<\/p>\n<pre class=\"brush: js;\">&gt; node index.js\r\n<\/pre>\n<p>Once the command completes you can navigate to the URL <code>http:\/\/localhost:8090\/index.html<\/code> in a browser.<br \/>\nThis takes you to the landing page of our application which looks like below:<\/p>\n<figure id=\"attachment_20811\" aria-describedby=\"caption-attachment-20811\" style=\"width: 762px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/02\/LandingPageNodejsHttpRequest-e1518013232935.jpg\"><img decoding=\"async\" class=\"size-full wp-image-20811\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/02\/LandingPageNodejsHttpRequest-e1518013232935.jpg\" alt=\"\" width=\"762\" height=\"580\" srcset=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/02\/LandingPageNodejsHttpRequest-e1518013232935.jpg 762w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/02\/LandingPageNodejsHttpRequest-e1518013232935-300x228.jpg 300w\" sizes=\"(max-width: 762px) 100vw, 762px\" \/><\/a><figcaption id=\"caption-attachment-20811\" class=\"wp-caption-text\">Landing Page &#8211; index.html<\/figcaption><\/figure>\n<h2>6. Download the Source Code<\/h2>\n<p>This wraps our look at making HTTP Requests with Node.js example.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here : <a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/02\/WCG-Node.js-Http-Request-Example.zip\"><strong>WCG &#8212; Node.js HTTP Request Example<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this post we take a look at how to make HTTP Requests with Node.js. This feature is key and important capability for a server side language. With Node.js being positioned as one of the best options for aggregating multiple services and high throughput due to its non blocking I\/O operations, it is an important &hellip;<\/p>\n","protected":false},"author":213,"featured_media":924,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[347],"class_list":["post-20519","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js","tag-nodejs"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Node.js HTTP Request Example - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"In this post we take a look at how to make HTTP Requests with Node.js. This feature is key and important capability for a server side language. With\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Node.js HTTP Request Example - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"In this post we take a look at how to make HTTP Requests with Node.js. This feature is key and important capability for a server side language. With\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2018-02-12T14:15:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/nodejs-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Siddharth Seth\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Siddharth Seth\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/\"},\"author\":{\"name\":\"Siddharth Seth\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/9c939eb4c915443c7e493c813d979592\"},\"headline\":\"Node.js HTTP Request Example\",\"datePublished\":\"2018-02-12T14:15:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/\"},\"wordCount\":1017,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/nodejs-logo.jpg\",\"keywords\":[\"nodejs\"],\"articleSection\":[\"Node.js\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/\",\"name\":\"Node.js HTTP Request Example - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/nodejs-logo.jpg\",\"datePublished\":\"2018-02-12T14:15:27+00:00\",\"description\":\"In this post we take a look at how to make HTTP Requests with Node.js. This feature is key and important capability for a server side language. With\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/nodejs-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/nodejs-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/javascript\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Node.js\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/javascript\/node-js\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Node.js HTTP Request Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/9c939eb4c915443c7e493c813d979592\",\"name\":\"Siddharth Seth\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/86a5133a5e9d79f7997e2649b1afe58e895c0d88df47b3359103ec4c1a2077d6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/86a5133a5e9d79f7997e2649b1afe58e895c0d88df47b3359103ec4c1a2077d6?s=96&d=mm&r=g\",\"caption\":\"Siddharth Seth\"},\"description\":\"Siddharth is a Software Development Professional with a Master degree in Computer Applications from IGNOU. He has over 14 years of experience. And currently focused on Software Architecture, Cloud Computing, JavaScript Frameworks for Client and Server, Business Intelligence.\",\"sameAs\":[\"https:\/\/www.webcodegeeks.com\"],\"url\":\"https:\/\/www.webcodegeeks.com\/author\/siddharth-seth\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Node.js HTTP Request Example - Web Code Geeks - 2026","description":"In this post we take a look at how to make HTTP Requests with Node.js. This feature is key and important capability for a server side language. With","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/","og_locale":"en_US","og_type":"article","og_title":"Node.js HTTP Request Example - Web Code Geeks - 2026","og_description":"In this post we take a look at how to make HTTP Requests with Node.js. This feature is key and important capability for a server side language. With","og_url":"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2018-02-12T14:15:27+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/nodejs-logo.jpg","type":"image\/jpeg"}],"author":"Siddharth Seth","twitter_card":"summary_large_image","twitter_creator":"@webcodegeeks","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Siddharth Seth","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/"},"author":{"name":"Siddharth Seth","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/9c939eb4c915443c7e493c813d979592"},"headline":"Node.js HTTP Request Example","datePublished":"2018-02-12T14:15:27+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/"},"wordCount":1017,"commentCount":0,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/nodejs-logo.jpg","keywords":["nodejs"],"articleSection":["Node.js"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/","url":"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/","name":"Node.js HTTP Request Example - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/nodejs-logo.jpg","datePublished":"2018-02-12T14:15:27+00:00","description":"In this post we take a look at how to make HTTP Requests with Node.js. This feature is key and important capability for a server side language. With","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/nodejs-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/nodejs-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/javascript\/node-js\/node-js-http-request-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"JavaScript","item":"https:\/\/www.webcodegeeks.com\/category\/javascript\/"},{"@type":"ListItem","position":3,"name":"Node.js","item":"https:\/\/www.webcodegeeks.com\/category\/javascript\/node-js\/"},{"@type":"ListItem","position":4,"name":"Node.js HTTP Request Example"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/9c939eb4c915443c7e493c813d979592","name":"Siddharth Seth","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/86a5133a5e9d79f7997e2649b1afe58e895c0d88df47b3359103ec4c1a2077d6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/86a5133a5e9d79f7997e2649b1afe58e895c0d88df47b3359103ec4c1a2077d6?s=96&d=mm&r=g","caption":"Siddharth Seth"},"description":"Siddharth is a Software Development Professional with a Master degree in Computer Applications from IGNOU. He has over 14 years of experience. And currently focused on Software Architecture, Cloud Computing, JavaScript Frameworks for Client and Server, Business Intelligence.","sameAs":["https:\/\/www.webcodegeeks.com"],"url":"https:\/\/www.webcodegeeks.com\/author\/siddharth-seth\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/20519","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/213"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=20519"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/20519\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/924"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=20519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=20519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=20519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}