<?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>Localhost &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/localhost/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>
		<item>
		<title>PHP Functions: realpath()</title>
		<link>/2010/05/22/php-functions-realpath/</link>
		<comments>/2010/05/22/php-functions-realpath/#respond</comments>
		<pubDate>Sat, 22 May 2010 07:30:55 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Completely]]></category>
		<category><![CDATA[Computer file]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Inter-process communication]]></category>
		<category><![CDATA[Localhost]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Software engineering]]></category>

		<guid isPermaLink="false">/?p=1530</guid>
		<description><![CDATA[Watch Out &#8211; Hard Code Perhaps every developer knows that hard coded paths are no good! The code&#8217;s good to be flexible and extensible, but what the way to achieve that? In a typically developed application you&#8217;re completely sure the way the folders are nested will be permanent and no change will occur, and that&#8217;s &#8230; <a href="/2010/05/22/php-functions-realpath/" class="more-link">Continue reading <span class="screen-reader-text">PHP Functions: realpath()</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2011/01/18/download-images-with-php/" rel="bookmark" title="Download Images with PHP">Download Images with PHP </a></li>
<li><a href="/2010/07/21/setting-up-global-cache-in-zend-framework/" rel="bookmark" title="Setting Up Global Cache in Zend Framework">Setting Up Global Cache in Zend Framework </a></li>
<li><a href="/2010/09/17/5-php-string-functions-you-need-to-know/" rel="bookmark" title="5 PHP String Functions You Need to Know">5 PHP String Functions You Need to Know </a></li>
<li><a href="/2011/07/29/php-strings-how-to-get-the-extension-of-a-file/" rel="bookmark" title="PHP Strings: How to Get the Extension of a File">PHP Strings: How to Get the Extension of a File </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Watch Out &#8211; Hard Code</h2>
<p>Perhaps every developer knows that hard coded paths are no good! The code&#8217;s good to be flexible and extensible, but what the way to achieve that? In a typically developed application you&#8217;re completely sure the way the folders are nested will be permanent and no change will occur, and that&#8217;s maybe true, but however don&#8217;t be completely sure.</p>
<h2>An Example</h2>
<p>Let&#8217;s take a typical example. You&#8217;ve to access a uploaded file with PHP. By default the files are uploaded in the /tmp folder with PHP given name. That&#8217;s why immediately after the upload (the form submit) you&#8217;ve to process the _POST and the _FILES arrays and perhaps move the uploaded file somewhere else.</p>
<p>However you&#8217;d like to access this file wherever the application is &#8211; on the production servers or on the development server or even on the localhost!</p>
<p>I ran into that kind of problem/task. The thing I&#8217;d like to achieve was a bit different. I constructed the path with the <a title="php: dirname()" href="http://php.net/manual/en/function.dirname.php" target="_blank"><strong>dirname()</strong></a> function, but there were still &#8216;/../../&#8217; chunks in it. So the solution is the <a title="php: realname()" href="http://www.php.net/manual/en/function.realpath.php" target="_blank"><strong>realpath()</strong></a> function that removes those chunks and converts the path into one &#8220;calculated&#8221; real path to the file.</p>
<p>Let me show you an example:</p>
<pre lang="php">
// get the uploaded file path
$scriptPath = dirname(__FILE__);

// get the realpath to avoid the /../ part of the path
// with dirname they remain in the path
$uploadFolderPath = realpath($scriptPath . '/../../folder_name/');
</pre>
<p>If you were using dirname() you&#8217;d get something like that:</p>
<pre>/folder1/folder2/folder3/../../folder2/file.txt</pre>
<p>Now with realpath() the result is:</p>
<pre>/folder1/folder2/file.txt</pre>
<p>That&#8217;s more clear and even both are working correctly I&#8217;d prefer the second one!</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2011/01/18/download-images-with-php/" rel="bookmark" title="Download Images with PHP">Download Images with PHP </a></li>
<li><a href="/2010/07/21/setting-up-global-cache-in-zend-framework/" rel="bookmark" title="Setting Up Global Cache in Zend Framework">Setting Up Global Cache in Zend Framework </a></li>
<li><a href="/2010/09/17/5-php-string-functions-you-need-to-know/" rel="bookmark" title="5 PHP String Functions You Need to Know">5 PHP String Functions You Need to Know </a></li>
<li><a href="/2011/07/29/php-strings-how-to-get-the-extension-of-a-file/" rel="bookmark" title="PHP Strings: How to Get the Extension of a File">PHP Strings: How to Get the Extension of a File </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/05/22/php-functions-realpath/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
