SlideShare uma empresa Scribd logo
1 de 19
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.

Mais conteúdo relacionado

Mais procurados

Node.js in a heterogeneous system
Node.js in a heterogeneous systemNode.js in a heterogeneous system
Node.js in a heterogeneous system
GeeksLab Odessa
 
download presentation
download presentationdownload presentation
download presentation
webhostingguy
 

Mais procurados (19)

Node.js in a heterogeneous system
Node.js in a heterogeneous systemNode.js in a heterogeneous system
Node.js in a heterogeneous system
 
Linux Security with SElinux
Linux Security with SElinuxLinux Security with SElinux
Linux Security with SElinux
 
Node intro
Node introNode intro
Node intro
 
GruntJS + Wordpress
GruntJS + WordpressGruntJS + Wordpress
GruntJS + Wordpress
 
Node Session - 4
Node Session - 4Node Session - 4
Node Session - 4
 
download presentation
download presentationdownload presentation
download presentation
 
Create a RESTful API with NodeJS, Express and MongoDB
Create a RESTful API with NodeJS, Express and MongoDBCreate a RESTful API with NodeJS, Express and MongoDB
Create a RESTful API with NodeJS, Express and MongoDB
 
Redis fundamental
Redis fundamentalRedis fundamental
Redis fundamental
 
Config websocket on apache
Config websocket on apacheConfig websocket on apache
Config websocket on apache
 
Complete MVC on NodeJS
Complete MVC on NodeJSComplete MVC on NodeJS
Complete MVC on NodeJS
 
你不知道的前端
你不知道的前端你不知道的前端
你不知道的前端
 
Node Session - 2
Node Session - 2Node Session - 2
Node Session - 2
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Writing and Publishing Puppet Modules
Writing and Publishing Puppet ModulesWriting and Publishing Puppet Modules
Writing and Publishing Puppet Modules
 
MySql cheat sheet
MySql cheat sheetMySql cheat sheet
MySql cheat sheet
 
Getting started with node JS
Getting started with node JSGetting started with node JS
Getting started with node JS
 
Package.json
Package.jsonPackage.json
Package.json
 
Webpack | Jakub Kulhan - Skrz.cz
Webpack | Jakub Kulhan - Skrz.czWebpack | Jakub Kulhan - Skrz.cz
Webpack | Jakub Kulhan - Skrz.cz
 
Sql related links
Sql related linksSql related links
Sql related links
 

Destaque

Destaque (11)

Node.js for beginner
Node.js for beginnerNode.js for beginner
Node.js for beginner
 
Build App with Nodejs - YWC Workshop
Build App with Nodejs - YWC WorkshopBuild App with Nodejs - YWC Workshop
Build App with Nodejs - YWC Workshop
 
The future of node
The future of nodeThe future of node
The future of node
 
Avoiding Callback Hell From JavaScript
Avoiding Callback Hell From JavaScriptAvoiding Callback Hell From JavaScript
Avoiding Callback Hell From JavaScript
 
Node js beginner
Node js beginnerNode js beginner
Node js beginner
 
Node js for beginners
Node js for beginnersNode js for beginners
Node js for beginners
 
Node.js tutoria for beginner
Node.js tutoria for beginnerNode.js tutoria for beginner
Node.js tutoria for beginner
 
Node js
Node jsNode js
Node js
 
The MEAN stack - SoCalCodeCamp - june 29th 2014
The MEAN stack - SoCalCodeCamp - june 29th 2014The MEAN stack - SoCalCodeCamp - june 29th 2014
The MEAN stack - SoCalCodeCamp - june 29th 2014
 
Becoming Node.js ninja on Cloud Foundry
Becoming Node.js ninja on Cloud FoundryBecoming Node.js ninja on Cloud Foundry
Becoming Node.js ninja on Cloud Foundry
 
The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js
The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.jsThe MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js
The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js
 

Semelhante a Nodejs quick start

Requiring your own files.pptx
Requiring your own files.pptxRequiring your own files.pptx
Requiring your own files.pptx
Lovely Professional University
 
From Dev to DevOps - Codemotion ES 2012
From Dev to DevOps - Codemotion ES 2012From Dev to DevOps - Codemotion ES 2012
From Dev to DevOps - Codemotion ES 2012
Carlos Sanchez
 
Continuous Delivery with Maven, Puppet and Tomcat - ApacheCon NA 2013
Continuous Delivery with Maven, Puppet and Tomcat - ApacheCon NA 2013Continuous Delivery with Maven, Puppet and Tomcat - ApacheCon NA 2013
Continuous Delivery with Maven, Puppet and Tomcat - ApacheCon NA 2013
Carlos Sanchez
 
From Dev to DevOps - FOSDEM 2012
From Dev to DevOps - FOSDEM 2012From Dev to DevOps - FOSDEM 2012
From Dev to DevOps - FOSDEM 2012
Carlos Sanchez
 
Node.js basics
Node.js basicsNode.js basics
Node.js basics
Ben Lin
 

Semelhante a Nodejs quick start (20)

Node.js in action
Node.js in actionNode.js in action
Node.js in action
 
Introduction to Node js for beginners + game project
Introduction to Node js for beginners + game projectIntroduction to Node js for beginners + game project
Introduction to Node js for beginners + game project
 
Node js
Node jsNode js
Node js
 
Local SQLite Database with Node for beginners
Local SQLite Database with Node for beginnersLocal SQLite Database with Node for beginners
Local SQLite Database with Node for beginners
 
May The Nodejs Be With You
May The Nodejs Be With YouMay The Nodejs Be With You
May The Nodejs Be With You
 
Ansible new paradigms for orchestration
Ansible new paradigms for orchestrationAnsible new paradigms for orchestration
Ansible new paradigms for orchestration
 
Requiring your own files.pptx
Requiring your own files.pptxRequiring your own files.pptx
Requiring your own files.pptx
 
From Dev to DevOps - Codemotion ES 2012
From Dev to DevOps - Codemotion ES 2012From Dev to DevOps - Codemotion ES 2012
From Dev to DevOps - Codemotion ES 2012
 
Continuous Delivery with Maven, Puppet and Tomcat - ApacheCon NA 2013
Continuous Delivery with Maven, Puppet and Tomcat - ApacheCon NA 2013Continuous Delivery with Maven, Puppet and Tomcat - ApacheCon NA 2013
Continuous Delivery with Maven, Puppet and Tomcat - ApacheCon NA 2013
 
Node.js
Node.jsNode.js
Node.js
 
Node js training (1)
Node js training (1)Node js training (1)
Node js training (1)
 
Introduction to node.js
Introduction to  node.jsIntroduction to  node.js
Introduction to node.js
 
From Dev to DevOps - FOSDEM 2012
From Dev to DevOps - FOSDEM 2012From Dev to DevOps - FOSDEM 2012
From Dev to DevOps - FOSDEM 2012
 
Nuxt.js - Introduction
Nuxt.js - IntroductionNuxt.js - Introduction
Nuxt.js - Introduction
 
Server Side Apocalypse, JS
Server Side Apocalypse, JSServer Side Apocalypse, JS
Server Side Apocalypse, JS
 
Node js (runtime environment + js library) platform
Node js (runtime environment + js library) platformNode js (runtime environment + js library) platform
Node js (runtime environment + js library) platform
 
Node JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web AppNode JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web App
 
Overview of Node JS
Overview of Node JSOverview of Node JS
Overview of Node JS
 
Nodejs first class
Nodejs first classNodejs first class
Nodejs first class
 
Node.js basics
Node.js basicsNode.js basics
Node.js basics
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 

Nodejs quick start

  • 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
  • 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
  • 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.