Thursday, June 12, 2014

Node.js load external html file

Example to load html code from external file, instead of code in js.


server.js
var http = require("http");
var fs = require("fs");

http.createServer(function(request, response){
    
    fs.readFile("page.html", function (err, data){
        if (err) {
            throw err; 
        }
        response.writeHead(200, {"Content-Type": "text/html"});
        response.write(data);
        response.end();
    });
    
}).listen(8888);

page.html
<html>
<head></head>
<body>
<h1>Hello World!</h1>
<p>from mobile-web-app.blogspot.com</p>
</body>
</html>


Return html code in Node.js

var http = require("http");

http.createServer(function(request, response){
    
    var htmlbody = 
        "<html>" +
        "<head>" +
        "</head>" +
        "<body>" +
        "<h1>Hello World!</h1>" +
        "<p>from mobile-web-app.blogspot.com</p>" + 
        "</body>" +
        "</html>";
        
    response.writeHead(200, {"Content-Type": "text/html"});
    response.write(htmlbody);
    response.end();
}).listen(8888);



Node.js load external html file

Wednesday, June 11, 2014

Get date in Node.js using Javascript

To get date in Node.js, we can use javascript's Date object.

Example:
var http = require("http");
var url = require("url");

http.createServer(function(request, response){
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Hello World!\n");
    response.write("from mobile-web-app.blogspot.com\n\n");
    
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth();
    var date = now.getDate();

    response.write(
        now + "\n");
    response.write(
        date + "/" + (month+1) + "/" + year + "\n");

    response.end();
}).listen(8888);



Tuesday, June 10, 2014

"Hello World", http server using Node.js

A simplest http server implemented using Node.js.


Create file node server.js.
var http = require("http");

http.createServer(function(request, response){
 response.writeHead(200, {"Content-Type": "text/plain"});
 response.write("Hello World! ");
 response.write("from mobile-web-app.blogspot.com");
 response.end();
}).listen(8888);

Run

$ node server.js


Then visit http://localhost:8888/ in browser to load "Hello World".

Monday, June 9, 2014

Implement fade-in/fade-out effect with jQuery

Example to implement fade-in/fade-out effect using jQuery:


<!DOCTYPE html>
<head>
</head>
<body>
    <h1 id="titleh1">Fade-in/Fade-out example</h1>
    <button id="go">Run</button>

    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    <script>
        $(function(){

            function runFadeOutFadeIn(){

                $("#titleh1").fadeTo(
                    "slow", 0, function(){
                        $("#titleh1").fadeTo(
                            "slow", 1, function(){});
                    });
            }
            
            $("#go").click(
                function(){
                    runFadeOutFadeIn();
                });
        });
    </script>
</body>
</html>

Tuesday, May 13, 2014

The Mystery And Magic Of APIs

What's an application programming interface, or API? You may not realize it, but every time you're using an app, you're connecting to all kinds of APIs. In this video, ReadWrite editor-in-chief explains what APIs are and why they matter to the Internet. (Presented by Intel)

ReadWriteExplain: The Mystery And Magic Of APIs

Wednesday, April 30, 2014

Where is variable defined, in JavaScript scope.

Whether you're coming to JavaScript from another language, or you're learning JavaScript as your first language, the way scope works -- that is, when and where your variables are defined -- might surprise you.