Free AI web copilot to create summaries, insights and extended knowledge, download it at here
2497
Abstract
lo World’ will be printed out on the screen</p><h1 id="2b96">Specifying The Header</h1><p id="8371"><i>response.writeHead() sends header response to the request. Or used to set the status code and create HTTP headers.</i></p><p id="4f20">If you want to specify the server response to be displayed in HTML. You need to tell the header that the <b>content type should be HTML</b></p><div id="d0bf"><pre>http.createServer(<span class="hljs-function">(<span class="hljs-params">req,res</span>)=></span> {
res.writeHead(<span class="hljs-number">200</span>,{<span class="hljs-string">'Content-Type'</span>:<span class="hljs-string">'text/html'</span>,
<span class="hljs-string">'Content-Length'</span>:body.<span class="hljs-built_in">length</span>})
..</pre></div><p id="4364">The first parameter of the <i>res.writeHead() </i>is the<b> response code</b>. It can be <b>2xx</b>, <b>3xx</b>,<b>4xx,</b> or <b>5xx.</b></p><p id="7e56">The second parameter is the <b>options. </b>Here we specified that the content type is HTML i.e our response will be displayed in HTML format, and that the length of the content will be equal to the length of your body.</p><p id="6124">You can even change headers using the <i>response argument </i>properties:</p><div id="0417"><pre><span class="hljs-attr">response.writeHead</span> = <span class="hljs-number">400</span></pre></div><h1 id="7968">HTTP Server Requests</h1><blockquote id="6988"><p>The <i>req </i>parameter of the <i>http.createServer </i>method has all the information about the incoming request for eg. request payload, headers, URL etc.</p></blockquote><p id="d56b">The <code><b><i>req</i></b></code> parameter has the following properties</p><ul><li><code><b><i>request.headers</i></b></code>: Information about the request headers such as Connection, Host, Authorization, etc.</li><li><code><b><i>request.method</i></b></code> : Information about the incoming request methods such as GET, POST, PUT, DELETE, OPTIONS, HEAD, etc.</li><li><code><b><i>request.url</i></b></code> : Information about the incoming request URL, such as <code><b>/accounts, /users, /messages</b></code> etc</li></ul><h2 id="6469">Example</h2>
<figure id="9b6c">
<div>
<div>
<iframe class="gist-iframe" src="/gist/HussainArif12/0906ff8ef80dd77d0018833590595326.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe>
Options
</div></div></figure><h1 id="489a">Processing Incoming Request Body in the Server</h1><p id="f1b8">Let’s say that the client has sent data, how do we process it?</p><h2 id="f8a0">Example</h2>
<figure id="619c">
<div>
<div>
<iframe class="gist-iframe" src="/gist/HussainArif12/1c8664cb262d0e3569aa00cf98aa7b89.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="5553">Executing this code requires a bit different way:</p><p id="9b9a">If you are on Windows, first you need to install a tool called <a href="https://curl.haxx.se/windows/">cURL</a> and then run the code through the command line.</p><p id="2a45">To send data, open another command line window, and write:</p><div id="347b"><pre><span class="hljs-attribute">curl</span> -x POST -d “key-value” localhost:<span class="hljs-number">3000</span></pre></div><p id="cbeb">Another alternative to cURL is <a href="https://www.postman.com/product/api-client">Postman</a></p><p id="9bdc">On your previous command line window, you will find the data that you sent through cURL. In this case, it is <code><b>key-value</b></code></p><h1 id="0978">Further Resources</h1><ul><li><a href="https://stackabuse.com/how-to-start-a-node-server-examples-with-the-most-popular-frameworks/">How to start a Node Server</a></li><li><a href="https://www.tutorialsteacher.com/nodejs/create-nodejs-web-server">Create Node.js Web Server</a></li></ul><p id="cda5">In the <a href="https://readmedium.com/introduction-to-express-js-246191ec05f2">next article</a>, we’ll use Express to make our request handling not only easier, but have shorter lines of code in the process.</p><p id="a18c">That’s all for today. Thank you for making it to the end!</p><p id="cb34">Stay home, stay safe!</p>
<figure id="d261">
<div>
<div>
<img class="ratio" src="http://placehold.it/16x9">
<iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fc36271%3Fas_embed%3Dtrue&dntp=1&display_name=Upscribe&url=https%3A%2F%2Fupscri.be%2Fc36271&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&schema=upscri" allowfullscreen="" frameborder="0" height="400" width="800">
</div>
</div>
</figure></iframe></div></div></figure></article></body>