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:
No comments:
Post a Comment