Tuesday, 2 June 2015

Node JS Express Mongo

This blog will help you to create a NodeJS application with the help of express, jade and mongo DB


1. Install Node JS
Go to link http://nodejs.org/ and click the Install button. It will identify your workstations operating system and start download an msi file.
After the file is downloaded install it at you preferred location.





2. Install Express plugin
Express is a web application framework for Node JS. It enables us to create simple web application very quickly. Since it is not packages with standard Node JS, we need to install it.
Create a directory where you wish to keep all your Node JS related projects
browse it in command propmt and type
C:\Softwares\nodejs>npm install -g express

3. Install Express Generator plugin
C:\Softwares\nodejs>npm install -g express-generator

4. Create Project
Now is the time to create the actual application. Fire below command.
C:\Softwares\nodejs>express login
This will create directory login in your current folder with all the dependencies required.

5. Install Project
Now its time to install the project. change directory to the login folder in command prompt and fire the below command.
C:\Softwares\nodejs\login>npm install
Thats it. Your application is ready to be tested.

6. Start the application
To start the web server. Fire the below command.
C:\Softwares\nodejs\login>npm start

< application-name@0.0.1 start C:\Softwares\nodejs\login
< node ./bin/www

5. in package.json
"mongodb": "*",
"mongoskin": "*"
6.
npm install
7.
npm start
8.
http://localhost:3000/
9. create folder data 10.
mongod --dbpath c:\node\nodetest1\data
11. cmd ->
mongo
12.
use login
13.
db.userdetails.insert({ "username" : "admin", "password" : "admin","name":"hunaid" })
db.userdetails.find().pretty()
14. In app.js
// Database
var mongo = require('mongoskin');
var db = mongo.db("mongodb://localhost:27017/nodetest2", {native_parser:true});
above
var routes = require('./routes/index');
// Make our db accessible to our router
app.use(function(req,res,next){
    req.db = db;
    next();
});
above
app.use('/', routes);
app.use('/users', users);
15 in index.jade create a login form 16. create login post method in index.js rener it to user page 17. create user.jade in views folder 18.
db.userdetails.find( { username: { $eq: 'admin' },password: { $eq: 'admin' }  } )

No comments:

Post a comment

Share the post