Wednesday, February 15, 2012

NodeJs : Create A Basic Html Server Using Simple File Read Operation

NodeJs : Create A Basic Html Server Using Simple File Read Operation

Here, I would like to implement a server which is read static html file and rendering other items like css, javascript and images to the client (browser you are using). To do this, I am going to use "Asynchronous File Read Operation". Because of my knowledge, I would believe NodeJs, only allow another task when it busy with file Read/Write operation. Here I’m going with this asynchronous operation to make NodeJs effective. To know more about NodeJs file system click here http://nodejs.org/docs/v0.3.1/api/fs.html .

And I want to my server also handle "File Not Found". Here I have showed my folder/directory structure in my xp system.



And following code has written in index.js located in the path "C:/…./htmlserver/folder".



And index.html looks like as follows,

And styles.css is


Here styles_1.css 



Why I'm writing the css in different file means just I want my server to handle multiple and different browser request. In the above index.html I have include nonexisting.js "script" tag because just I would like to setup my server to handle 404 error also. See following figure, there is no ".js" file in js folder.



There is a file page_404.html. I use this whenever invalid page request hit my server.



Output:






Note: Please write your queries and suggestion as comment.

Friday, February 3, 2012

NodeJs : REST Call Using http Library

Here I am showing how to bypass the incoming request to another site or how to do the operation like curl.

In the following code, your local Node server got incoming request via port 1337 and send it to "Google Maps API". And write the response (which is get from API) in your browser. 
Now write following code.

Output:



Note: Please write your queries and suggestion as comment.

Wednesday, February 1, 2012

NodeJs : Create and Install Simple Module using npm - Node Package Manager

Let’s see, how to create simple module and how to install the module using npm (Node Package Manager).

In NodeJs, module is a piece of code/function which is doing specific task or operation and modules are higher level of reusable code. Modules are expanding features of project and they are created in either in JavaScript or c++. 

Here I am going to show creation and installation of "Hello world" module. Let’s assume that we already created a project namely "helloworld_proj" and the project has a file namely "index.js" with following code.
 

Now I am going to create a module namely "helloworld_mod" to display "Hello World" in browser.

1. Create a folder named "helloworld_mod" in your project directory. Obviously you can keep your module anywhere in your system when it is in development stage.

2. And create a file "package.json"  and a folder named "lib" within the module folder.

3. Write following line of code in "package.json"  file.


Note: To know more about "package.json" click here http://docs.nodejitsu.com/articles/getting-started/npm/what-is-the-file-package-json.

4. And create a file named "index.js" within "lib" folder. Now the path of "index.js" file would be "C:\Program Files\nodejs\node_builds\helloworld_proj\helloworld_mod\lib\index.js".

5. And then write following code in index.js (located within "lib" folder).


6. Now your module ready to install. To install go to DOS-Command prompt and set base path to your project by using command >cd "C:\Program Files\nodejs\node_builds\helloworld_proj".

7. Now type the command >npm root. This gives path which is currently selected nodejs servers installation path. If it shows the path "C:\Program Files\nodejs\node_modules", that means NodeJs server install the module common for all project. Here assume that you want the "helloworld_mod" module only for "helloworld_proj".


Note : To know more about npm click here http://npmjs.org/doc/README.html

8. To install module within my "helloworld_proj", create a folder named "node_modules" within the project folder. Now the path would be "C:\Program Files\nodejs\node_builds\helloworld_proj\node_modules".

9. Now again type the command >npm root and verify the installation path. It should be "C:\Program Files\nodejs\node_builds\helloworld_proj\node_modules".

10. Now type the command >npm install helloworld_mod. This command install helloworld_mod and displays the detail "helloworld_mod@0.0.1 ./node_modules/helloworld_mod".



11. To confirm the installation, go to "C:\Program Files\nodejs\node_builds\helloworld_proj\node_modules" and you can see new folder with name "helloworld_mod" which is contain files and folders similar to your original module folder.

12. Now go to project folder "C:\Program Files\nodejs\node_builds\helloworld_proj"  and edit the file "index.js" as follows.


13. Start the server by type the command >node index.js . Now type the url http://127.0.0.1:1337/ in browser and press enter. Then the browser says "helloworld_mod says Hello World".  


For more details click here http://nodejs.org/docs/v0.4.4/api/modules.html.


Note: Please write your queries and suggestion as comment.