Skip to content

Commit 4068660

Browse files
committed
initial commit
1 parent a559fb1 commit 4068660

13 files changed

+4410
-2
lines changed

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2010-2013 Alibaba.com, Inc.
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19+
DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
7-days-nodejs
1+
七天学会NodeJS
22
=============
33

4-
七天学会NodeJS
4+
这是一本NodeJS新手入门教程,涵盖了使用NodeJS开发各种程序所必要的知识,请点击以下链接阅读。
5+
6+
> [http://nqdeng.github.io/7-days-nodejs/](http://nqdeng.github.io/7-days-nodejs/)

index.html

Lines changed: 1978 additions & 0 deletions
Large diffs are not rendered by default.

index.tpl

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
<!doctype HTML>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>七天学会NodeJS</title>
6+
<style>
7+
body {
8+
font-family: Tahoma;
9+
font-size: 10pt;
10+
line-height: 170%;
11+
padding: 0 10pt;
12+
}
13+
14+
nav {
15+
background: gray;
16+
color: white;
17+
overflow-x: hidden;
18+
overflow-y: auto;
19+
position: fixed;
20+
top: 0;
21+
left: 0;
22+
bottom: 0;
23+
width: 240px;
24+
}
25+
26+
header {
27+
padding-left: 240px;
28+
}
29+
30+
header h1 {
31+
background: #ccffcc;
32+
border-radius: 15px;
33+
color: #006600;
34+
font-size: 18pt;
35+
margin: 0;
36+
padding: 1.5em;
37+
text-align: center;
38+
}
39+
40+
article {
41+
padding-left: 240px;
42+
}
43+
44+
article h2 {
45+
border-bottom: dotted 1px #777;
46+
color: #006600;
47+
font-size: 12pt;
48+
margin: 4em 0 1em 0;
49+
padding: 0 0 0.3em 0;
50+
}
51+
52+
article h3 {
53+
border-left: 4px solid #ff6666;
54+
color: #000;
55+
font-size: 11pt;
56+
line-height: 100%;
57+
margin: 1em 0;
58+
padding: 0 0 0 0.5em;
59+
}
60+
61+
article h4 {
62+
color: #000;
63+
font-size: 10pt;
64+
margin: 1em 0;
65+
padding: 0;
66+
}
67+
68+
article p {
69+
margin: 1em 0;
70+
}
71+
72+
article p code {
73+
background: #eee;
74+
border: 1px solid #ccc;
75+
}
76+
77+
article p strong {
78+
color: #f00;
79+
}
80+
81+
article pre {
82+
background: #eee;
83+
border-left: solid 2px #3c0;
84+
color: #000;
85+
margin: 1em 0;
86+
padding: 0 0 0 1em;
87+
overflow-x: auto;
88+
overflow-y: padding;
89+
}
90+
91+
article blockquote {
92+
background: #fff;
93+
border: dashed 1px #777;
94+
border-left: solid 2px #777;
95+
color: #000;
96+
margin: 0;
97+
padding: 0 0 0 1em;
98+
}
99+
100+
nav ul {
101+
margin: 10px;
102+
padding: 0;
103+
}
104+
105+
nav a {
106+
color: white;
107+
text-decoration: none;
108+
}
109+
110+
nav a:hover {
111+
text-decoration: underline;
112+
}
113+
114+
nav li {
115+
list-style: none;
116+
margin: 0;
117+
padding: 0;
118+
}
119+
120+
nav .level2 {
121+
font-size: 11pt;
122+
font-weight: bold;
123+
}
124+
125+
nav .level3 {
126+
padding-left: 1em;
127+
}
128+
129+
nav .level3:before {
130+
content: "» ";
131+
}
132+
133+
nav .level4 {
134+
padding-left: 2em;
135+
}
136+
137+
nav .level4:before {
138+
content: "";
139+
}
140+
141+
footer {
142+
padding-left: 240px;
143+
}
144+
</style>
145+
</head>
146+
<body>
147+
<header>
148+
<x-markdown src="section/00_header.md" />
149+
</header>
150+
<nav>
151+
<x-index />
152+
</nav>
153+
<article>
154+
<x-markdown src="section/01_getting_started.md" />
155+
<x-markdown src="section/02_code_management_and_deployment.md" />
156+
<x-markdown src="section/03_file.md" />
157+
<x-markdown src="section/04_network.md" />
158+
<x-markdown src="section/05_process.md" />
159+
<x-markdown src="section/06_async_programming.md" />
160+
<x-markdown src="section/07_example.md" />
161+
</article>
162+
<footer>
163+
<x-markdown src="section/99_footer.md" />
164+
</footer>
165+
</body>
166+
</html>

section/00_header.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
七天学会NodeJS
2+
======================

section/01_getting_started.md

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
NodeJS基础
2+
----------------------
3+
4+
### 什么是NodeJS
5+
6+
JS是脚本语言,脚本语言都需要一个解析器才能运行。对于写在HTML页面里的JS,浏览器充当了解析器的角色。而对于需要独立运行的JS,NodeJS就是一个解析器。
7+
8+
每一种解析器都是一个运行环境,不但允许JS定义各种数据结构,进行各种计算,还允许JS使用运行环境提供的内置对象和方法做一些事情。例如运行在浏览器中的JS的用途是操作DOM,浏览器就提供了`document`之类的内置对象。而运行在NodeJS中的JS的用途是操作磁盘文件或搭建HTTP服务器,NodeJS就相应提供了`fs``http`等内置对象。
9+
10+
### 有啥用处
11+
12+
尽管存在一听说可以直接运行JS文件就觉得很酷的同学,但大多数同学在接触新东西时首先关心的是有啥用处,以及能带来啥价值。
13+
14+
NodeJS的作者说,他创造NodeJS的目的是为了实现高性能Web服务器,他首先看重的是事件机制和异步IO模型的优越性,而不是JS。但是他需要选择一种编程语言实现他的想法,这种编程语言不能自带IO功能,并且需要能良好支持事件机制。JS没有自带IO功能,天生就用于处理浏览器中的DOM事件,并且拥有一大群程序员,因此就成为了天然的选择。
15+
16+
如他所愿,NodeJS在服务端活跃起来,出现了大批基于NodeJS的Web服务。而另一方面,NodeJS让前端众如获神器,终于可以让自己的能力覆盖范围跳出浏览器窗口,更大批的前端工具如雨后春笋。
17+
18+
因此,对于前端而言,虽然不是人人都要拿NodeJS写一个服务器程序,但简单可至使用命令交互模式调试JS代码片段,复杂可至编写工具提升工作效率。
19+
20+
NodeJS生态圈正欣欣向荣。
21+
22+
### 如何安装
23+
24+
#### 安装程序
25+
26+
NodeJS提供了一些安装程序,都可以在[nodejs.org](http://nodejs.org/download/)这里下载并安装。
27+
28+
Windows系统下,选择和系统版本匹配的`.msi`后缀的安装文件。Mac OS X系统下,选择`.pkg`后缀的安装文件。
29+
30+
#### 编译安装
31+
32+
Linux系统下没有现成的安装程序可用,虽然一些发行版可以使用`apt-get`之类的方式安装,但不一定能安装到最新版。因此Linux系统下一般使用以下方式编译方式安装NodeJS。
33+
34+
1. 确保系统下g++版本在4.6以上,python版本在2.6以上。
35+
36+
2.[nodejs.org](http://nodejs.org/download/)下载`tar.gz`后缀的NodeJS最新版源代码包并解压到某个位置。
37+
38+
3. 进入解压到的目录,使用以下命令编译和安装。
39+
40+
$ ./configure
41+
$ make
42+
$ sudo make install
43+
44+
### 如何运行
45+
46+
打开终端,键入`node`进入命令交互模式,可以输入一条代码语句后立即执行并显示结果,例如:
47+
48+
$ node
49+
> console.log('Hello World!');
50+
Hello World!
51+
52+
如果要运行一大段代码的话,可以先写一个JS文件再运行。例如有以下`hello.js`
53+
54+
function hello() {
55+
console.log('Hello World!');
56+
}
57+
hello();
58+
59+
写好后在终端下键入`node hello.js`运行,结果如下:
60+
61+
$ node hello.js
62+
Hello World!
63+
64+
#### 权限问题
65+
66+
在Linux系统下,使用NodeJS监听80或443端口提供HTTP(S)服务时需要root权限,有两种方式可以做到。
67+
68+
一种方式是使用`sudo`命令运行NodeJS。例如通过以下命令运行的`server.js`中有权限使用80和443端口。一般推荐这种方式,可以保证仅为有需要的JS脚本提供root权限。
69+
70+
$ sudo node server.js
71+
72+
另一种方式是使用`chmod +s`命令让NodeJS总是以root权限运行,具体做法如下。因为这种方式让任何JS脚本都有了root权限,不太安全,因此在需要很考虑安全的系统下不推荐使用。
73+
74+
$ sudo chown root /usr/local/bin/node
75+
$ sudo chmod +s /usr/local/bin/node
76+
77+
### 模块
78+
79+
编写稍大一点的程序时一般都会将代码模块化。在NodeJS中,一般将代码合理拆分到不同的JS文件中,每一个文件就是一个模块,而文件路径就是模块名。
80+
81+
在编写每个模块时,都有`require``exports``module`三个预先定义好的变量可供使用。
82+
83+
#### require
84+
85+
`require`函数用于在当前模块中加载和使用别的模块,传入一个模块名,返回一个模块导出对象。模块名可使用相对路径(以`./`开头),或者是绝对路径(以`/``C:`之类的盘符开头)。另外,模块名中的`.js`扩展名可以省略。以下是一个例子。
86+
87+
var foo1 = require('./foo');
88+
var foo2 = require('./foo.js');
89+
var foo3 = require('/home/user/foo');
90+
var foo4 = require('/home/user/foo.js');
91+
92+
// foo1至foo4中保存的是同一个模块的导出对象。
93+
94+
另外,可以使用以下方式加载和使用一个JSON文件,模块名中`.json`扩展名不可省略。
95+
96+
var data = require('./data.json');
97+
98+
#### exports
99+
100+
`exports`对象是当前模块的导出对象,用于导出模块公有方法和属性。别的模块通过`require`函数使用当前模块时得到的就是当前模块的`exports`对象。以下例子中导出了一个公有方法。
101+
102+
exports.hello = function () {
103+
console.log('Hello World!');
104+
};
105+
106+
#### module
107+
108+
通过`module`对象可以访问到当前模块的一些相关信息,但最多的用途是替换当前模块的导出对象。例如模块导出对象默认是一个普通对象,如果想改成一个函数的话,可以使用以下方式。
109+
110+
module.exports = function () {
111+
console.log('Hello World!');
112+
};
113+
114+
以上代码中,模块默认导出对象被替换为一个函数。
115+
116+
#### 模块初始化
117+
118+
一个模块中的JS代码仅在模块第一次被使用时执行一次,并在执行过程中初始化模块的导出对象。之后,缓存起来的导出对象被重复利用。
119+
120+
#### 主模块
121+
122+
通过命令行参数传递给NodeJS以启动程序的模块被称为主模块。主模块负责调度组成整个程序的其它模块完成工作。例如通过以下命令启动程序时,`main.js`就是主模块。
123+
124+
$ node main.js
125+
126+
#### 完整示例
127+
128+
例如有以下目录。
129+
130+
- /home/user/hello/
131+
- util/
132+
counter.js
133+
main.js
134+
135+
其中`counter.js`内容如下:
136+
137+
var i = 0;
138+
139+
function count() {
140+
return ++i;
141+
}
142+
143+
exports.count = count;
144+
145+
该模块内部定义了一个私有变量`i`,并在`exports`对象导出了一个公有方法`count`
146+
147+
主模块`main.js`内容如下:
148+
149+
var counter1 = require('./util/counter');
150+
var counter2 = require('./util/counter');
151+
152+
console.log(counter1.count());
153+
console.log(counter2.count());
154+
console.log(counter2.count());
155+
156+
运行该程序的结果如下:
157+
158+
$ node main.js
159+
1
160+
2
161+
3
162+
163+
可以看到,`counter.js`并没有因为被require了两次而初始化两次。
164+
165+
### 二进制模块
166+
167+
虽然一般我们使用JS编写模块,但NodeJS也支持使用C/C++编写二进制模块。编译好的二进制模块除了文件扩展名是`.node`外,和JS模块的使用方式相同。虽然二进制模块能使用操作系统提供的所有功能,拥有无限的潜能,但对于前端同学而言编写过于困难,并且难以跨平台使用,因此不在本教程的覆盖范围内。
168+
169+
### 小结
170+
171+
本章介绍了有关NodeJS的基本概念和使用方法,总结起来有以下知识点:
172+
173+
+ NodeJS是一个JS脚本解析器,任何操作系统下安装NodeJS本质上做的事情都是把NodeJS执行程序复制到一个目录,然后保证这个目录在系统PATH环境变量下,以便终端下可以使用`node`命令。
174+
175+
+ 终端下直接输入`node`命令可进入命令交互模式,很适合用来测试一些JS代码片段,比如正则表达式。
176+
177+
+ NodeJS使用[CMD](http://wiki.commonjs.org/)模块系统,主模块作为程序入口点,所有模块在执行过程中只初始化一次。
178+
179+
+ 除非JS模块不能满足需求,否则不要轻易使用二进制模块,否则你的用户会叫苦连天。

0 commit comments

Comments
 (0)