Nodejs quick start

1,179 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,179
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Nodejs quick start

  1. 1. Nodejs quick start
  2. 2. Hello world  console.log("Hello world!");
  3. 3. Run in command  Node your_js_file_name
  4. 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. 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. 6. expressjs  A framework for nodejs
  7. 7. install  Npm install –g express
  8. 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. 9. Install dependencies  Cd app_name && npm install
  10. 10. Run app  Node app
  11. 11. Create route  In app.js,add a line like followings:  Var hello = require(‘./routes/hello’)  App.get(‘/hello’,hello.index);
  12. 12. Create controller  In routes folder create hello.js  Exports.index = function(req,res){  Res.render(‘hello’,{title:’hello world demo’});  }
  13. 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. 14. Use models  No orm modules for nodejs,need pain sql except mongooesjs
  15. 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. 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. 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. 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. 19. Fin.

×