This document provides an overview of Node.js and how to use it for web development. It covers installing Node.js, the basic syntax and features of Node.js like modules, asynchronous programming. It also discusses using the NPM package manager and popular Node packages. Finally, it demonstrates how to build a basic web server and framework like Express along with integrating a database like MySQL.
4. ● Download source code
● Install
tar -zxf node-vx.x.x.tar.gz
cd node-vx.x.x
./configure
./configure --prefix=/opt/node
make
sudo make install
Node.js install - Install from Source
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. Basic of Node - 基本語法
● 載入模組
var http = require('http');
● 分享模組
export
module.export
● 註解
// This is a common
/* This is a common */
● 變數
name = value //global
var 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. Basic of Node - Exception
$ node 003-for3.js
/Users/jellychang/Workspaces/NodeWS/Sample/003-for3.js:1
process.argv ){
^
node.js:201
throw 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 trace
Stack trace DETAIL
錯誤原因
13. Basic of Node - Language Core
● 順序 (Sequence)
● 判斷 (If ... Then .... Else ....)
● 迴圈 (Loops)
14. Basic of Node -
Non-Block vs Sequence
$ cat 000-sequence.js
console.log('The start of node...');
for(i = 0; i< 3 ; i++ ){
console.log('=>' + i);
}
console.log('The end of node...');
$ node 000-sequence.js
The start of node...
=>0
=>1
=>2
The end of node...
#cat 000-nonblock2.js
setTimeout(function(){
console.log("bar...");
}, 1000);
console.log("foo...");
$ node 000-unblock2.js
foo... (will wait...)
bar...
15. Basic of Node - if condition
if (CONDITION) {
RULE
} else if {
RULE
} else {
RULE
}
#!/usr/bin/env node
//003-if.js
if ( 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.js
not 1 or 2...
$ ./003-if.js 1
input 1...
$ ./003-if.js 2
input 2...
16. Basic of Node - switch
switch ( EVENT ) {
case EVENT_VALUE:
PROCESS
break;
... (other case)
default:
PROCESS
break;
}
#003-switch.js
console.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. Basic of Node - for loop
for ( i in SET ) {
PROCESS...
}
#003-for.js
for ( 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. Basic of Node - for loop
for ( KEY=VALUE;
BREAK_CONDITION;
BREAK_RULE) {
PROCESS
}
#003-for2.js
for ( i=0 ; i < 5 ; i++ ){
console.log(i);
}
$ node 003-for2.js
0
1
2
3
4
19. Basic of Node - while loop
while ( CONDITION ){
PROCESS
[BREAK_RULE]
}
$ cat 003-while.js
i = 5;
while(i < process.argv[2]){
console.log('-->' + i);
i++;
}
$ node 003-while.js 10
-->5
-->6
-->7
-->8
-->9
21. Install NPM
● >= Node.js 0.6.*
○ Congratulation, in already included in the Node.js
● < Node.js 0.6.*
● One Line Install
curl 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.exe
lives
23. NPM advance commands
● npm list
List the detail help contents
● npm install <pkg. name> -gd
Install the package into global folder.
○ Basicly, the npm install <pkg. name> will install the
package into the user home directory, like:
$HOME/node_modules/*
○ Npm related information will store in $HOME/.npm
24. Best NPM packages
● View module: jade, express, ejs
● MySQL connector: mysql
● Mailer: nodemailer
● NoSQL: felix-couchdb, mongodb
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>
!!!
html
head
title= title
link(rel='stylesheet',
href='/stylesheets/style.css')
body
block content
31. Using ejs as View Engine - View
/* file: /view/index.ejs */
<h1><%= title %></h1>
<p>Welcome to <%= title %></p>
extends layout
block content
h1= title
p Welcome to #{title}