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

Nodejs quick start

on

  • 999 views

 

Statistics

Views

Total Views
999
Views on SlideShare
998
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 Nodejs quick start Presentation Transcript

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