<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>port While &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/port-while/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>Diving into Node.js &#8211; Very First App</title>
		<link>/2010/11/19/diving-into-node-js-very-first-app/</link>
		<comments>/2010/11/19/diving-into-node-js-very-first-app/#comments</comments>
		<pubDate>Fri, 19 Nov 2010 09:55:36 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[web development]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[Computer networking]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[Hypertext Transfer Protocol]]></category>
		<category><![CDATA[Internet protocols]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[Localhost]]></category>
		<category><![CDATA[Network architecture]]></category>
		<category><![CDATA[Node.js]]></category>
		<category><![CDATA[port While]]></category>
		<category><![CDATA[server on nodejs]]></category>
		<category><![CDATA[Server-side JavaScript]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">/?p=2066</guid>
		<description><![CDATA[What do I have till now? After Node.js is istalled, described in my previous post, I can simply run this command: stoimenpopov:~# node server.js and this will start the server with the code within server.js. But what&#8217;s the code of server.js? Following the instructions of Node&#8217;s homepage and most of the tutorials I&#8217;ve found, I &#8230; <a href="/2010/11/19/diving-into-node-js-very-first-app/" class="more-link">Continue reading <span class="screen-reader-text">Diving into Node.js &#8211; Very First App</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/12/02/diving-into-node-js-a-long-polling-example/" rel="bookmark" title="Diving into Node.js &#8211; A Long Polling Example">Diving into Node.js &#8211; A Long Polling Example </a></li>
<li><a href="/2010/11/16/diving-into-node-js-introduction-and-installation/" rel="bookmark" title="Diving into Node.js &#8211; Introduction &#038; Installation">Diving into Node.js &#8211; Introduction &#038; Installation </a></li>
<li><a href="/2010/03/23/read-remote-file-content-type-with-zend_http_client/" rel="bookmark" title="Read Remote File Content-Type with Zend_Http_Client">Read Remote File Content-Type with Zend_Http_Client </a></li>
<li><a href="/2011/04/13/post-with-zend_http_client/" rel="bookmark" title="POST with Zend_Http_Client">POST with Zend_Http_Client </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>What do I have till now?</h2>
<p>After Node.js is istalled, described in <a href="/2010/11/16/diving-into-node-js-introduction-and-installation/" target="_self">my previous post</a>, I can simply run this command:</p>
<blockquote><p>stoimenpopov:~# node server.js</p></blockquote>
<p>and this will start the server with the code within server.js. But what&#8217;s the code of server.js?</p>
<p>Following the instructions of <a title="Nodejs" href="http://nodejs.org/" target="_blank">Node&#8217;s homepage</a> and most of the tutorials I&#8217;ve found, I can simply copy/paste the code from the first lines of Node&#8217;s page:</p>
<pre lang="javascript">var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');</pre>
<p>There are several things I find interesting in this code, making it different from JavaScript as we know it. First of all what is</p>
<blockquote><p>var http = require(&#8216;http&#8217;)</p></blockquote>
<p>and why I need it? What is the purpouse of 8124 and 127.0.0.1?</p>
<p>Node is built in modules and to use one of them you must first include it with <strong>require</strong>. Just like the example above with require(&#8216;http&#8217;). In the same manner you can include every module of Node.</p>
<p>What are the Node&#8217;s modules are pretty well described in <a title="Node.js API" href="http://nodejs.org/docs/v0.2.5/api.html" target="_blank">the API page</a>. Well I&#8217;d like to say that the API page is quite insufficient. That is very bad, cause most of the code you&#8217;ll need developing a node.js applicatoin isn&#8217;t described/explained there.<span id="more-2066"></span></p>
<p>However is more than obvious that 8124 is the port, while 127.0.0.1 is the IP address of the server, which in this case is the localhost. Thus after running the server and typing http://127.0.0.1:8124/ into your browser you&#8217;ll get &#8220;Hello World&#8221;. This is not enough when the server is running on a remote machine, while you&#8217;ve to access it from your local computer.</p>
<h2>The first server</h2>
<p>As I described the code above shows you an example server. In fact the API page shows a slightly different code:</p>
<pre lang="javascript">var http = require('http');

http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.end('Hello World\n');
}).listen(8124);

console.log('Server running at http://127.0.0.1:8124/');</pre>
<p>Here the line:</p>
<pre lang="javascript">}).listen(8124);</pre>
<p>omits the address of the server. It appears to be non-required and the server is supposed to listen on this machine on the given port. To be honest I&#8217;m not sure why you&#8217;ve to write the server address, except for limit the possible address to this one. However in my case the server was installed on a remote machine indeed, while I was trying to access it from my local machine. So here are some base steps to do before accomplishing this.</p>
<h3>1. Changing the IP address</h3>
<p>First of all you can omit the IP address. This can help you move the code from one server to another without changing the code.</p>
<pre lang="javascript">}).listen(8124, ip_address);</pre>
<p>possibly a better solution is:</p>
<pre lang="javascript">}).listen(8124);</pre>
<p>Node can listen on whatever port you say so this is up to you to decide which port should be opened.</p>
<h3>2. Opening the port</h3>
<p>While everything&#8217;s on the localhost you wont have problems with port opening. If you&#8217;d like to access it remotely however, you&#8217;ve to open the port. In other words http://127.0.0.1:8124/ is OK without port opening, but http://example.com:8124/ &#8211; is not!</p>
<p>To open a port in UNIX/Linux you&#8217;ve to type this command in the terminal:</p>
<pre lang="bash">iptables -A INPUT -p tcp --dport 8124 -j ACCEPT</pre>
<p>don&#8217;t forget to change the port number from 8124 to whatever port number you&#8217;re using.</p>
<h3>3. The first AJAX call</h3>
<p>We still cannot make the first AJAX call to the Node server. Here I&#8217;m using <a title="jQuery" href="http://jquery.com/" target="_blank">jQuery</a> because of the easy AJAX interface.</p>
<pre lang="javascript">$(document).ready(function() {
	$.ajax({
    	// setup the server address
        url : 'http://example:8124/',
        success : function(response) {
        	// on success
        	alert(response);
		},
        error : function() {
        	// on error
        }
    });
});</pre>
<p>After executing this code in a browser there wont be a response. That is because of the remote AJAX calls restriction. To overcome this you&#8217;ve to change the returned header from the Node&#8217;s server, by adding this:</p>
<pre lang="javascript">'Access-Control-Allow-Origin' : '*'</pre>
<p>Now server.js will look like this:</p>
<pre lang="javascript">var http = require('http');

http.createServer(function (request, response) {
	response.writeHead(200, {
		'Content-Type': 'text/plain',
		'Access-Control-Allow-Origin' : '*'
	});
	response.end('Hello World\n');
}).listen(8124);</pre>
<p>you can now either load http://example.com:8124/ into your browser or to make the AJAX call. In both cases the answer will be &#8220;Hello World&#8221;</p>
<h2>Summary</h2>
<p>Now you have a very simple Node.js server. Well it is nothing special as it responses with &#8220;Hello World&#8221; on every request, but it works remotely.</p>
<p>Soon I&#8217;ll describe how to change the server so it can waits to respond until some event is emitted &#8211; which is the real power of Node.js.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/12/02/diving-into-node-js-a-long-polling-example/" rel="bookmark" title="Diving into Node.js &#8211; A Long Polling Example">Diving into Node.js &#8211; A Long Polling Example </a></li>
<li><a href="/2010/11/16/diving-into-node-js-introduction-and-installation/" rel="bookmark" title="Diving into Node.js &#8211; Introduction &#038; Installation">Diving into Node.js &#8211; Introduction &#038; Installation </a></li>
<li><a href="/2010/03/23/read-remote-file-content-type-with-zend_http_client/" rel="bookmark" title="Read Remote File Content-Type with Zend_Http_Client">Read Remote File Content-Type with Zend_Http_Client </a></li>
<li><a href="/2011/04/13/post-with-zend_http_client/" rel="bookmark" title="POST with Zend_Http_Client">POST with Zend_Http_Client </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/11/19/diving-into-node-js-very-first-app/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>
