Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Node.js in action

8,407 views

Published on

Node.js的基本介紹,包含基本語法、NPM、Express入門等

Published in: Technology

Node.js in action

  1. 1. Node.js in ActionSimon Su @ MiCloud
  2. 2. Objective● Node.js Installation● Node.js Basic● Node.js Web Framework - Express
  3. 3. Install Node.js
  4. 4. ● Download source code● Installtar -zxf node-vx.x.x.tar.gzcd node-vx.x.x./configure./configure --prefix=/opt/nodemakesudo make installNode.js install - Install from Source
  5. 5. ● Mac安裝Node.js:○ Mac版本安裝程式為node-vx.x.x.pkg,執行pkg安裝程式後,將會有安裝程序帶領您安裝完Node.js○ 其他安裝支援:MacPort, homebrew● SmartOS安裝Node.js:○ pkgin search node○ pkgin install nodejs-0.4.9● Windows安裝Node.js:○ v0.6.x前安裝Node.js於Windows上必須先行安裝 cygwin 然後採用source install方式安裝○ v0.6.x後提供msi安裝檔案Node.js install - All OS
  6. 6. Node.js install - v0.6.8+ support
  7. 7. Node.js vs Javascript
  8. 8. Node.js vs JavascriptNode.js● Server side languageJavaScript● Client side language
  9. 9. Node.js Basic
  10. 10. Node.js Read-Eval-Print-Loop (REPL)$ node> var os=require(os);undefined> os.hostname()SimonAIR.local>undefined>(^C again to quit)>$指令列直接執行node鍵入測試指令使用Ctri + C (兩次) 離開
  11. 11. Basic of Node - 基本語法● 載入模組var http = require(http);● 分享模組exportmodule.export● 註解// This is a common/* This is a common */● 變數name = value //globalvar name = value● 執行node nodejs-file-name.js● 全域物件(Global Objects)● global● process● console● Buffer● require()● require.resolve()● require.cache● __filename● __dirname● module● exports● setTimeout(cb, ms)● clearTimeout(t)● setInterval(cb, ms)● clearInterval(t)
  12. 12. Basic of Node - Exception$ node 003-for3.js/Users/jellychang/Workspaces/NodeWS/Sample/003-for3.js:1process.argv ){^node.js:201throw e; // process.nextTick error, or error event on first tick^SyntaxError: Unexpected token )at Module._compile (module.js:427:25)at Object..js (module.js:450:10)at Module.load (module.js:351:31)at Function._load (module.js:310:12)at Array.0 (module.js:470:10)at EventEmitter._tickCallback (node.js:192:40)錯誤程式區段與位置資訊Stack traceStack trace DETAIL錯誤原因
  13. 13. Basic of Node - Language Core● 順序 (Sequence)● 判斷 (If ... Then .... Else ....)● 迴圈 (Loops)
  14. 14. Basic of Node -Non-Block vs Sequence$ cat 000-sequence.jsconsole.log(The start of node...);for(i = 0; i< 3 ; i++ ){console.log(=> + i);}console.log(The end of node...);$ node 000-sequence.jsThe start of node...=>0=>1=>2The end of node...#cat 000-nonblock2.jssetTimeout(function(){console.log("bar...");}, 1000);console.log("foo...");$ node 000-unblock2.jsfoo... (will wait...)bar...
  15. 15. Basic of Node - if conditionif (CONDITION) {RULE} else if {RULE} else {RULE}#!/usr/bin/env node//003-if.jsif ( process.argv[2] == 1 ) {console.log(input 1...);} else if (process.argv[2] == 2) {console.log(input 2...);} else {console.log(not 1 or 2...);}$ ./003-if.jsnot 1 or 2...$ ./003-if.js 1input 1...$ ./003-if.js 2input 2...
  16. 16. Basic of Node - switchswitch ( EVENT ) {case EVENT_VALUE:PROCESSbreak;... (other case)default:PROCESSbreak;}#003-switch.jsconsole.log(--> + process.argv[2]);switch ( process.argv[2] ) {case 1:console.log(==>1);break;case 10:console.log(==>10);break;default:console.log(default...);break;}$ node 003-switch.js 1-->1==>1
  17. 17. Basic of Node - for loopfor ( i in SET ) {PROCESS...}#003-for.jsfor ( i in process.argv ){console.log(--> + i += + process.argv[i]);}$ node 003-for.js 1 2 3-->0=node-->1=/.../Sample/003-for.js-->2=1-->3=2-->4=3
  18. 18. Basic of Node - for loopfor ( KEY=VALUE;BREAK_CONDITION;BREAK_RULE) {PROCESS}#003-for2.jsfor ( i=0 ; i < 5 ; i++ ){console.log(i);}$ node 003-for2.js01234
  19. 19. Basic of Node - while loopwhile ( CONDITION ){PROCESS[BREAK_RULE]}$ cat 003-while.jsi = 5;while(i < process.argv[2]){console.log(--> + i);i++;}$ node 003-while.js 10-->5-->6-->7-->8-->9
  20. 20. Node Ecosystem - NPMNode Package Management
  21. 21. Install NPM● >= Node.js 0.6.*○ Congratulation, in already included in the Node.js● < Node.js 0.6.*● One Line Installcurl http://npmjs.org/install.sh | sh● More Than One Line Install○ Get the code... https://github.com/isaacs/npm○ Make & Make install it...● Windows Install○ Download a zip file from http://npmjs.org/dist/○ Unpack it in the same folder where node.exelives
  22. 22. NPM Basic UsageSearch:npm search [some search terms ...]ex: npm search expressInstall:npm install <name>ex: npm install express● Remove:npm uninstall <name>[@<version> [<name>[@<version>]...]ex: npm uninstall express
  23. 23. NPM advance commands● npm listList the detail help contents● npm install <pkg. name> -gdInstall the package into global folder.○ Basicly, the npm install <pkg. name> will install thepackage into the user home directory, like:$HOME/node_modules/*○ Npm related information will store in $HOME/.npm
  24. 24. Best NPM packages● View module: jade, express, ejs● MySQL connector: mysql● Mailer: nodemailer● NoSQL: felix-couchdb, mongodb
  25. 25. Node.js Web Fraework -Express / Ejs
  26. 26. Basic Web Servervar http = require(http);http.createServer(function (req, res) {res.writeHead(200, {Content-Type: text/plain});res.end(Hello Worldn);}).listen(1337, 127.0.0.1);console.log(Server running at http://127.0.0.1:1337/);
  27. 27. 3Steps using express/ejs framework1. Install express, ejs, jadenpm install express ejs jade -gdPS: Install specific version of modulenpm install express@2.5.9 [-gd]2. Create your projectexpress <your project name>ex: express TestPrj3. Sync project modulecd <your project name> && npm installex: cd TestPrj && npm install
  28. 28. Testing of Created Project# cd $YOUR_WORKSPACE/TestPrj# node app.js# cd $YOUR_WORKSPACE/TestPrj# ls -ltr總計 24drwxr-xr-x 5 root root 316 2012-03-30 00:41 publicdrwxr-xr-x 2 root root 182 2012-03-30 00:41 routesdrwxr-xr-x 2 root root 252 2012-03-30 00:41 views-rw-r--r-- 1 root root 153 2012-03-30 00:41 package.json-rw-r--r-- 1 root root 783 2012-03-30 00:41 app.jsdrwxr-xr-x 5 root root 303 2012-03-30 00:41 node_modules# node app.jsExpress server listening on port 3000 in development mode
  29. 29. Using ejs as View Engine - Config//file: app.js//Configure route settingapp.configure(function(){app.set(views, __dirname + /views);app.set(view engine, ejs);app.use(express.bodyParser());app.use(express.cookieParser());//app.use(express.session({ secret: "keyboard cat" }));app.use(express.methodOverride());app.use(app.router);app.use(express.static(__dirname + /public));app.set("view options", {layout : true});});//Configure routingapp.get(/, function(req, res){res.render(index, {title: Express});});var express = require(express), routes = require(./routes);var app = module.exports = express.createServer();// Configurationapp.configure(function(){app.set(views, __dirname + /views);app.set(view engine, jade);app.use(express.bodyParser());app.use(express.methodOverride());app.use(app.router);app.use(express.static(__dirname + /public));});app.configure(development, function(){app.use(express.errorHandler({ dumpExceptions:true, showStack: true }));});app.configure(production, function(){app.use(express.errorHandler());});// Routesapp.get(/, routes.index);app.listen(3000, function(){console.log("Express server listening on port %d in%s mode", app.address().port, app.settings.env);});
  30. 30. Using ejs as View Engine - layout/* file: /view/layout.ejs */<!DOCTYPE html><html><head><meta name="viewport" content="width=device-width, initial-scale=1"><title><%= title %></title><link rel=stylesheet href=/stylesheets/style.css /></head><body><!-- /page --><div data-role="page"><!-- /header --><div data-role="header"><h1>My Title</h1></div><!-- /content --><%- body %></div></body></html>!!!htmlheadtitle= titlelink(rel=stylesheet,href=/stylesheets/style.css)bodyblock content
  31. 31. Using ejs as View Engine - View/* file: /view/index.ejs */<h1><%= title %></h1><p>Welcome to <%= title %></p>extends layoutblock contenth1= titlep Welcome to #{title}
  32. 32. Configure REST routing// file: app.jsapp.get(/getUsedExam/:classid, refEngin.getUsedExam);app.get(/getUsedExam/:classid/:teacherid, refEngin.getUsedExam);app.get(/getUsedExam/:classid/:teacherid/:examid, refEngin.getUsedExam);// file: routes/someRouter.jsexports.getUserdExam = function(req, res, next) {var classid = req.params.classid;...(skip)console.log(classid: + classid );res.render(simeViewEjsFile, {result: classid});});};
  33. 33. Node.js MySQLConnector
  34. 34. Installing MySQL connector$ npm install mysqlnpm http GET https://registry.npmjs.org/mysqlnpm http 304 https://registry.npmjs.org/mysqlnpm http GET https://registry.npmjs.org/hashish/0.0.4npm http 304 https://registry.npmjs.org/hashish/0.0.4npm http GET https://registry.npmjs.org/traversenpm http 304 https://registry.npmjs.org/traversemysql@0.9.5 ./node_modules/mysql└── hashish@0.0.4
  35. 35. Using MySQL connector - Config$ vi mysql-config.jsvar db_options = {host: your.database.address,port: your_database_port,user: access_database_name,password: access_database_password,database: database_that_will_be_use};var mysql = new require(mysql), db = null;if(mysql.createClient) {db = mysql.createClient(db_options);} else {db = new mysql.Client(db_options);db.connect(function(err) {if(err) {console.error(connect db + db.host + error: + err);process.exit();}});}exports.db = db;
  36. 36. /* file: routes/someRoute.js */var config = require(./mysql-config), db = config.db;exports.checkExamStatus = function(req, res, next) {var sql = select * from some_talbe a where a.some_column = ? and a.some_column2 = ?;var some_condition = req.params.some_condition;var cond = [some_condition, some_condition2];console.log(Query: + sql );console.log(Cond: + cond);db.query(sql, cond, function(err, rows, fiels) {if(err) return next(err);res.render(simpleJsonResult, {result: rows});});};/* file: view/simpleJsonResult.ejs */<%-JSON.stringify(result)%>Using MySQL connector - Query
  37. 37. /* file: routes/someRoute.js */var config = require(./mysql-config), db = config.db;exports.checkExamStatus = function(req, res, next) {var sql = select * from some_talbe a where 1=1 ;var some_condition = req.params.some_condition;var cond = new Array();if (req.params.some_condition != null) {sql += and a.some_column = ? ;cond.push(some_condition);}console.log(Query: + sql );console.log(Cond: + cond);db.query(sql, cond, function(err, rows, fiels) {if(err) return next(err);res.render(simpleJsonResult, {result: rows});});};Using MySQL connector - Query 2
  38. 38. Enjoy Your NodeEND

×