Nodejs quick start
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Nodejs quick start

on

  • 1,048 views

 

Statistics

Views

Total Views
1,048
Views on SlideShare
1,047
Embed Views
1

Actions

Likes
0
Downloads
3
Comments
0

1 Embed 1

https://twitter.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Nodejs quick start Presentation Transcript

  • 1. Nodejs quick start
  • 2. Hello world  console.log("Hello world!");
  • 3. Run in command  Node your_js_file_name
  • 4. Node package manager  Npm install package_name  Install in node_moudles of your application  Using it like following:  Var mysql = require(“mysql”);  Maintain dependenices in package.json like  {  “name”:”node”,  “version”:”1.0.0”,  “dependencies”:{  “mysql”:”0.6.0”  }  }
  • 5. Useful packages  Supervisor  Node-inspector:  Npm install node-inspector –g  Start your application  Node –debug your_js_file  Start node-inspector node-inspector Go to http://localhost:8080 to debug
  • 6. expressjs  A framework for nodejs
  • 7. install  Npm install –g express
  • 8. Create new app in command  Express –sessions –css stylus –ejs app_name  Oprtions:  -h –help output usage information  -V –version outpu the version number  -s –sessions add session support  -e –ejs add ejs engine support (defaults to jade)  -J –jshtml addjshtml engine support  -H –Hogan add Hogan.js engine support  -c –css add css support(less|stylus)(defaults to plain css)  -f –force force on –non-empty directory
  • 9. Install dependencies  Cd app_name && npm install
  • 10. Run app  Node app
  • 11. Create route  In app.js,add a line like followings:  Var hello = require(‘./routes/hello’)  App.get(‘/hello’,hello.index);
  • 12. Create controller  In routes folder create hello.js  Exports.index = function(req,res){  Res.render(‘hello’,{title:’hello world demo’});  }
  • 13. Create view  In views folder add new file name hello.ejs  <html>  <head>  <meta charset="UTF-8"/>  <title><%= title %></title>  </head>  <body>  <%= title %>  </body>  </html>
  • 14. Use models  No orm modules for nodejs,need pain sql except mongooesjs
  • 15. mongoose  Model for nodejs  Installation: install mongodb run server mongod –dbpath “the/db/path” edit package.json file,add dependencies … dependencies:{ … “connect”:”*”, “mongoose”:”*” } … Npm install
  • 16. Create models file  //database setting  var mongoose = require('mongoose');  mongoose.connect('mongodb://localhost/hello-world');  //all mongoose object start with schema  var Schema = mongoose.Schema;  var ObjectId = Schema.ObjectId;  var kittySchema = mongoose.Schema({  name:String  });  var Kitten = mongoose.model('Kitten', kittySchema);  exports.Kitten = Kitten;
  • 17. Modify hello.js  //import objects in models.js  var models = require('../models');  var Kitten = models.Kitten;  exports.index = function(req,res){  var kitten = new Kitten({name:Math.random()}).save(function(err,kitten,count){  if (!err){  console.log(kitten.name+' saved');  }  else{  console.log(err);  }  });  res.render('hello',{title:'hello world demo'});  }  exports.list = function(req, res){  var kitten_list = Kitten.find(function(err,list,count){  res.render('list',{list:list});  });  }
  • 18. create list.ejs file  //import objects in models.js  var models = require('../models');  var Kitten = models.Kitten;  exports.index = function(req,res){  var kitten = new Kitten({name:Math.random()}).save(function(err,kitten,count){  if (!err){  console.log(kitten.name+' saved');  }  else{  console.log(err);  }  });  res.render('hello',{title:'hello world demo'});  }  exports.list = function(req, res){  var kitten_list = Kitten.find(function(err,list,count){  res.render('list',{list:list});  });  }
  • 19. Fin.