How to create a project/site with nodejs and express

First check the versions of node and npm by the following commands..

node -v

npm -v

Now lets say your workspace is in d:/workspace

here you need to create a basic site with nodejs and express..

In order to do that, first install express in your machine globally..

you can achieve this by following commands

npm install -g express - old process
D:\workspace>npm install -g express-generator (preferable)
D:\workspace>express nodetest

With this you can see a list of folders will be created like shown below

26-05-2014 16-58-07

You’ll note that the express installation routine created a file called package.json in your nodetest directory. Open this up in a text editor and it’ll look like this:

26-05-2014 17-02-09

This is a basic JSON file describing our app and its dependencies. We need to add a few things to it. Specifically, calls for MongoDB and Monk. Let’s make our dependencies object look like this:

"dependencies":{"express":"~4.0.0","static-favicon":"~1.0.0","morgan":"~1.0.0","cookie-parser":"~1.0.1","body-parser":"~1.0.0","debug":"~0.7.4","jade":"~1.3.0","mongodb":"*","monk":"*"}

INSTALL DEPENDENCIES


 

Now we’ve defined our dependencies and we’re ready to go. Note that the asterisks tell NPM “just get the latest version” when you run the install, which we’re about to do.

 


Return to your command prompt, cd to your nodetest directory, and type this:

COMMAND D:\Workspace\NODETEST\
D:\workspace\nodetest>npm install

It’s going to print out a ton of stuff. That’s because it’s reading the JSON file we just edited and installing all the stuff listed in the dependencies object (yes, including Express – we installed the top level stuff using the –g flag, but we still have to install some necessary code for this particular project). Once NPM has run its course, you should have a node_modules directory which contains all of our dependencies for this tutorial.

You now have a fully-functioning app ready and waiting to run. Before we do that, though, we need to do one quick thing to prepare for setting up our database later. Still in your nodetest1 directory, type this:

COMMAND C:\NODE\NODETEST1\
C:\node\nodetest1>mkdir data

 

That’s where we’re eventually going to store our MongoDB data. If that directory doesn’t exist, the database server will choke when we go to run it later. We’re not doing anything with it right now, though, so let’s test out our web server! Type the following:

 

COMMAND C:\NODE\NODTEST1\
C:\node\nodetest1>npm start

Hit enter. You’ll get this:

27-05-2014 12-09-30

Everything working? Awesome! Open a browser and head for http://localhost:3000 where you will see a welcome to Express page.

27-05-2014 12-10-41

 

You are now running your own Node JS webserver, with the Express engine and Jade HTML preprocessor installed 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s