theJava
theJava

Reputation: 15044

Couple of questions in node.js

// Server.js

var http = require('http');
var path = require('path');
var fs = require('fs');

http.createServer(function (request, response) {

    console.log('request starting...');

    var filePath = '.' + request.url;
    if (filePath == './')
        filePath = './index.html';

    path.exists(filePath, function(exists) {

        if (exists) {
            fs.readFile(filePath, function(error, content) {
                if (error) {
                    response.writeHead(500);
                    response.end();
                }
                else {
                    response.writeHead(200, { 'Content-Type': 'text/html' });
                    response.end(content, 'utf-8');
                }
            });
        }
        else {
            response.writeHead(404);
            response.end();
        }
    });

}).listen(8125);

console.log('Server running at http://127.0.0.1:8125/');

// index.html

<html>
    <head>
        <title>Rockin' Page</title>
        <link type="text/css" rel="stylesheet" href="style.css" />
        <script type="text/javascript" src="jquery-1.7.1.min.js"></script>   
        </head>
    <body>
        <p>This is a page. For realz, yo.</p>
    </body>
    <script type="text/javascript">
        $(document).ready(function() {
            alert('happenin');
        });
    </script>
</html>

I am able to run my static page, but i have couple of questions down the line.

  1. What do i do next? i mean what to develop and what to learn? i am confused.. what is the difference i am doing with my current webserver.
  2. Is node.js just an replacement of my Apache Webserver.
  3. Can anyone clearly explain me the main purpose of nodejs

Upvotes: 0

Views: 255

Answers (3)

phihag
phihag

Reputation: 288260

node.js is a platform (language, library, and interpreter), and Turing-complete, i.e. you can do anything with it. Most likely, you'll want a web application which is interactive in some way. Have a look at examples like a chat room. There are also lots of other resources on how to get started.

In the end, it's up to you what you want your site to be. A chatroom? A forum? A search engine? A multiplayer game? If you just want to transfer static files (i.e. have no need for server state or communication between clients), there's no need to use node.js.

Upvotes: 3

Jim Schubert
Jim Schubert

Reputation: 20367

Questions

  1. What do i do next? i mean what to develop and what to learn? i am confused.. what is the difference i am doing with my current webserver.
  2. Is node.js just an replacement of my Apache Webserver.
  3. Can anyone clearly explain me the main purpose of nodejs

Answers

  1. Start with some simple examples and/or tutorials. I've forked Mastering Node on github, which is a quick read but is also still a work in progress. I've used expressjs for quickly creating static sites (like my online resume). I also use node.js and nodeunit for testing JavaScript or performing scripting tasks that could otherwise be done in bash, php, batch, perl, etc.

  2. node.js gives an IO wrapper to Google's V8 JavaScript engine. This means that JavaScript is not bound to a web browser and can interact with any type of IO. That means files, sockets, processes (phihag's Turing-complete answer). It can do pretty much anything.

  3. The main purpose of nodejs is that IO code is evented and (mostly) non-blocking. For example, in ASP.NET, when a web server receives a request that request's thread is blocked until all processing is complete (unless processed by an asynchronous handler, which is the exception not the rule). In node.js (express, railwayjs, etc.), the request processing is handled by events and callbacks. Code is executed asynchronously and callbacks are executed when complete. This is similar to the asynchronous pages of ASP.NET, the main difference being that node.js and web frameworks on top of it don't create millions of threads. I believe the threading issue is discussed in Ryan's video.

Upvotes: 2

gastonfartek
gastonfartek

Reputation: 348

here is an excellent video from nodejs creator ryan ... http://www.youtube.com/watch?v=jo_B4LTHi3I

it explains what it is with code examples it is really good.

here are some more resources that you can take a look at

http://blog.jayway.com/2011/05/15/a-not-very-short-introduction-to-node-js/

http://www.nodetuts.com/

http://www.howtonode.org/

Upvotes: 2

Related Questions