Nodejs quick start
-
Upload
guangyao-cao -
Category
Technology
-
view
845 -
download
0
Transcript of Nodejs quick start
![Page 1: Nodejs quick start](https://reader036.fdocument.pub/reader036/viewer/2022081504/5587824ad8b42a8f7d8b458f/html5/thumbnails/1.jpg)
Nodejs quick start
![Page 2: Nodejs quick start](https://reader036.fdocument.pub/reader036/viewer/2022081504/5587824ad8b42a8f7d8b458f/html5/thumbnails/2.jpg)
Hello world
console.log("Hello world!");
![Page 3: Nodejs quick start](https://reader036.fdocument.pub/reader036/viewer/2022081504/5587824ad8b42a8f7d8b458f/html5/thumbnails/3.jpg)
Run in command
Node your_js_file_name
![Page 4: Nodejs quick start](https://reader036.fdocument.pub/reader036/viewer/2022081504/5587824ad8b42a8f7d8b458f/html5/thumbnails/4.jpg)
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”
}
}
![Page 5: Nodejs quick start](https://reader036.fdocument.pub/reader036/viewer/2022081504/5587824ad8b42a8f7d8b458f/html5/thumbnails/5.jpg)
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
![Page 6: Nodejs quick start](https://reader036.fdocument.pub/reader036/viewer/2022081504/5587824ad8b42a8f7d8b458f/html5/thumbnails/6.jpg)
expressjs
A framework for nodejs
![Page 7: Nodejs quick start](https://reader036.fdocument.pub/reader036/viewer/2022081504/5587824ad8b42a8f7d8b458f/html5/thumbnails/7.jpg)
install
Npm install –g express
![Page 8: Nodejs quick start](https://reader036.fdocument.pub/reader036/viewer/2022081504/5587824ad8b42a8f7d8b458f/html5/thumbnails/8.jpg)
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
![Page 9: Nodejs quick start](https://reader036.fdocument.pub/reader036/viewer/2022081504/5587824ad8b42a8f7d8b458f/html5/thumbnails/9.jpg)
Install dependencies
Cd app_name && npm install
![Page 10: Nodejs quick start](https://reader036.fdocument.pub/reader036/viewer/2022081504/5587824ad8b42a8f7d8b458f/html5/thumbnails/10.jpg)
Run app
Node app
![Page 11: Nodejs quick start](https://reader036.fdocument.pub/reader036/viewer/2022081504/5587824ad8b42a8f7d8b458f/html5/thumbnails/11.jpg)
Create route
In app.js,add a line like followings:
Var hello = require(‘./routes/hello’)
App.get(‘/hello’,hello.index);
![Page 12: Nodejs quick start](https://reader036.fdocument.pub/reader036/viewer/2022081504/5587824ad8b42a8f7d8b458f/html5/thumbnails/12.jpg)
Create controller
In routes folder create hello.js
Exports.index = function(req,res){
Res.render(‘hello’,{title:’hello world demo’});
}
![Page 13: Nodejs quick start](https://reader036.fdocument.pub/reader036/viewer/2022081504/5587824ad8b42a8f7d8b458f/html5/thumbnails/13.jpg)
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>
![Page 14: Nodejs quick start](https://reader036.fdocument.pub/reader036/viewer/2022081504/5587824ad8b42a8f7d8b458f/html5/thumbnails/14.jpg)
Use models
No orm modules for nodejs,need pain sql except mongooesjs
![Page 15: Nodejs quick start](https://reader036.fdocument.pub/reader036/viewer/2022081504/5587824ad8b42a8f7d8b458f/html5/thumbnails/15.jpg)
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
![Page 16: Nodejs quick start](https://reader036.fdocument.pub/reader036/viewer/2022081504/5587824ad8b42a8f7d8b458f/html5/thumbnails/16.jpg)
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;
![Page 17: Nodejs quick start](https://reader036.fdocument.pub/reader036/viewer/2022081504/5587824ad8b42a8f7d8b458f/html5/thumbnails/17.jpg)
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});
});
}
![Page 18: Nodejs quick start](https://reader036.fdocument.pub/reader036/viewer/2022081504/5587824ad8b42a8f7d8b458f/html5/thumbnails/18.jpg)
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});
});
}
![Page 19: Nodejs quick start](https://reader036.fdocument.pub/reader036/viewer/2022081504/5587824ad8b42a8f7d8b458f/html5/thumbnails/19.jpg)
Fin.