Extending Our NodeJS static + Dynamic Server

Here, we would be extending / simplifying our server that we had made in our last post here.
Objectives –

  • We had a static URL handler which was invoked when URL pattern had /static/. It made the design very specific and not generic.
  • Need to make sure that in case there is no URL pattern handler for a given URL, a file for same name must be looked. If there is no such file, just raise 404 error.

 Get the server

Fully Functional NodeJS static + Dynamic Server

Off late I have seen many people struggling with creating a static Node server, also there have been questions like “How to have static & Dynamic files served by Node”. So here I have a fully functional server that can serve both static as well as dynamic files. Off course there is no need as there are many frameworks that can take care of these things. But always good to do things on your own to learn a new language or as we say Reinventing the wheels.
Below is the server that decides what content to serve based on URL. I have used /static/ to mark static content from URL pattern. That is any URL having this pattern would be considered static and would be served from file system.
Read Complete entry

Getting Started with MongoDB and NodeJS

Before we kick start Node application lets start by creating a MongoDB database and credentials. Though creating database, could have been achieved by using Node, but that would not provide us with user & password. And I believe no one would want their database to be accessible to everyone. 😀
I hope you have got MongoDB installed on your machine, just start up the server and open command prompt.

>mongo
MongoDB shell version: 2.0.6
connecting to: test
>use supaldubey
switched to db supaldubey
>db.addUser('supal', 'key')

Node Code Follows!

Your First NodeJs Application

If you are looking for a “Hello World!” example, I am sorry, you have landed to a wrong page, for hello World, your best bet is the Nodejs site.
Lets just have a brief discussion on what Node (Developers kinda like this name than Nodejs) really is. Node is a platform based on Google’s high performance (& open source) V8 Java script engine, if you are a “Geek” you must have read about it as Chrome uses it as well. Node being build on the Javascript engine, provides a JS like environment for Server side development. Yes, you read this right. Node is Server side Javascript framework.
Get the first Node App!