Tag Archives: http

Diving into Node.js – Very First App

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’s the code of server.js?

Following the instructions of Node’s homepage and most of the tutorials I’ve found, I can simply copy/paste the code from the first lines of Node’s page:

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/');

There are several things I find interesting in this code, making it different from JavaScript as we know it. First of all what is

var http = require(‘http’)

and why I need it? What is the purpouse of 8124 and 127.0.0.1?

Node is built in modules and to use one of them you must first include it with require. Just like the example above with require(‘http’). In the same manner you can include every module of Node.

What are the Node’s modules are pretty well described in the API page. Well I’d like to say that the API page is quite insufficient. That is very bad, cause most of the code you’ll need developing a node.js applicatoin isn’t described/explained there. Continue reading Diving into Node.js – Very First App

Replace the Broken Images with a Default Image with JavaScript

There is cool JavaScript trick that helps you catch broken images. You know that when the image doesn’t exist, the http path to the image returns 404 or the path is wrong, the browser doesn’t display nothing in the most cases. As MSIE is always different it displays an ugly icon saying that there is not an image to load

MSIE broken image icon

and that is really bad!

There’s a Quick Fix …

Simply add an onerror handler on the IMG tag

<img src="http://..../broken_url.jpg" onerror="this.src='path_to_default_image'" />

Send Authenticated POST Request with Zend_Http_Client


There is an easy way to send requests with Zend_Http_Client either in GET and POST. Actually you can request not only with GET and POST methods, but with any other http request method.

$httpClient = new Zend_Http_Client('http://....');
// request via post
$response = $httpClient->request(Zend_Http_Client::POST);
// or get
$response = $httpClient->request(Zend_Http_Client::GET);

However the interesting and useful thing is that you can perform authenticated requests:

$httpClient = new Zend_Http_Client('http://username:password@example.com');
// or define it later
$httpClient = new Zend_Http_Client('http://example.com');
$httpClient->setAuth('username', 'password');

That’s an opportunity to send/receive authenticated POST requests.

$httpClient = new Zend_Http_Client('http://username:password@example.com');
// now post
$httpClient->request(Zend_Http_Client::POST);

Check YouTube Video Existence with Zend_Gdata_YouTube

The Video Has Been Removed?!

Have you ever seen the famous message on YouTube: “This video has been removed by the user.”, but what if you’re trying to grab that video via the Zend_Gdata_YouTube?

That can happen when you’re reading the feed for a channel or a user’s list of videos. In the feed everything’s OK – once the video is uploaded it appears into the feed. After that the user removes the video and visiting the link from the feed you’ll get the message above.

Actually what happens when you try to read this “video entry” within a Zend Framework’s application. There must be some way to catch this message?

Trying to Catch

The answer is pretty simple! Zend_Gdata_YouTube’s throwing an exception and it can be easily cached. Here’s some source code:

$apiKey = 'your_api_key';
$client = new Zend_Http_Client();
$gdata = new Zend_Gdata_YouTube($client, 'my-app', null, $apiKey);
try {
       $gdata->getVideoEntry($video_id);
} catch(Zend_Gdata_App_Exception $e) {
       echo $e->getMessage();
}

All the Site in … One Request

Is it possible?

Yes it is! Actually I stumbled these days on a video where one of the guys talked about a quite interesting technique, that all the site was sent in one response from the server. But how is it possible? Actually everything is collected on the server side, i.e. with PHP which groups everything within a string. Obviously the images are base64ed. Than everything is send to the client with appropriate delimiters and mime types, and the client separates the string and build ups the page.

Problems

Of course there are some problems. First of all, as you may guess, MSIE doesn’t support base64. Another bad thing is that this isn’t cacheable.

One Good Use

There is however a good place to use this technique. In mobile versions. There is no much need of caches and most of all MSIE is not there!