-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0caedab
commit 6e367ee
Showing
7 changed files
with
448 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ NodeJs小册 | |
|
||
关于 <about/index> | ||
快速开始 <quick-start/index> | ||
Node.js之旅 <tour/index> | ||
标准库 <stdlib/index> | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,3 @@ | |
:maxdepth: 3 | ||
|
||
文件读写 <read-write> | ||
一坨猪屎 <zhushi> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,6 @@ | |
文件系统 <fs/index> | ||
|
||
|
||
|
||
.. comments | ||
comment something out below | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
|
||
======== | ||
文件模块 | ||
======== | ||
|
||
|
||
Node.js作为文件服务器 | ||
==================== | ||
|
||
`Node.js` 的文件系统模块允许你调用计算机上的文件系统,来操作文件。 | ||
|
||
我们使用 ``require`` 函数来引入文件系统模块。 | ||
|
||
.. code-block:: javascript | ||
var fs = require('fs') | ||
文件系统的常用用法有: | ||
|
||
- 读取文件 | ||
- 创建文件 | ||
- 更新文件 | ||
- 删除文件 | ||
- 重命名文件 | ||
|
||
读取文件 | ||
======= | ||
|
||
可以使用 ``fs.readFile()`` 方法来操作电脑上的文件 | ||
|
||
假设我们有以下的 ``HTML`` 文件(和 `Node.js` 文件置于同一目录下) | ||
|
||
.. code-block:: html | ||
|
||
<!--demofile1.html--> | ||
|
||
<html> | ||
<body> | ||
<h1>My Header</h1> | ||
<p>My paragraph.</p> | ||
</body> | ||
</html> | ||
|
||
创建一个读取该 ``html`` 文件的 `Node.js` 文件,并且返回读取的内容。 | ||
|
||
.. code-block:: javascript | ||
var http = require('http'); | ||
var fs = require('fs'); | ||
http.createServer(function (req, res) { | ||
fs.readFile('demofile1.html', function(err, data) { | ||
res.writeHead(200, {'Content-Type': 'text/html'}); | ||
res.write(data); | ||
res.end(); | ||
}); | ||
}).listen(8080); | ||
保存上述代码到 ``demo_readfile.js`` ,并执行。 | ||
|
||
.. code-block:: shell | ||
C:\Users\Your Name>node demo_readfile.js | ||
访问 `<http://localhost:8080>`_ ,将会看到和例子一样的结果。 | ||
|
||
创建文件 | ||
======= | ||
|
||
创建文件,有以下的方法可以使用: | ||
|
||
- fs.appendFile() | ||
- fs.open() | ||
- fs.writeFile() | ||
|
||
``fs.appendFile()`` 方法可以往文件添加指定的内容,如果文件不存在,将会被创建。 | ||
|
||
.. code-block:: javascript | ||
// 用fs.appendFile()来创建文件 | ||
var fs = require('fs'); | ||
fs.appendFile('mynewfile1.txt', 'Hello content!', function (err) { | ||
if (err) throw err; | ||
console.log('Saved!'); | ||
}); | ||
``fs.open()`` 方法的第二个参数 ``flag`` ,如果取值为 ``w`` 的话,也就是 ``writing`` ,表示打开该文件是为了写入。 | ||
如果该文件不存在,将会创建一个空文件。 | ||
|
||
.. code-block:: javascript | ||
var fs = require('fs'); | ||
fs.open('mynewfile2.txt', 'w', function (err, file) { | ||
if (err) throw err; | ||
console.log('Saved!'); | ||
}); | ||
``fs.writeFile()`` 方法可以替换已存在文件的内容,如果该文件不存在,一个新文件将会被创建,并且包含了写入的内容。 | ||
|
||
.. code-block:: javascript | ||
var fs = require('fs'); | ||
fs.writeFile('mynewfile3.txt', 'Hello content!', function (err) { | ||
if (err) throw err; | ||
console.log('Saved!'); | ||
}); | ||
更新文件 | ||
======= | ||
|
||
可以调用以下方法来更新文件: | ||
|
||
- fs.appendFile() | ||
|
||
- fs.writeFile() | ||
|
||
``fs.appendFile()`` 方法可以往文件的末尾添加指定的内容: | ||
|
||
.. code-block:: javascript | ||
// 将"this is my text."这句话添加到文件"mynewfile1.txt"的结尾 | ||
var fs = require('fs'); | ||
fs.appendFile('mynewfile1.txt', ' This is my text.', function (err) { | ||
if (err) throw err; | ||
console.log('Updated!'); | ||
}); | ||
``fs.writeFile()`` 方法可以替换文件中的内容: | ||
|
||
.. code-block:: javascript | ||
// 替换"mynewfile3.txt"的内容 | ||
var fs = require('fs'); | ||
fs.writeFile('mynewfile3.txt', 'This is my text', function (err) { | ||
if (err) throw err; | ||
console.log('Replaced!'); | ||
}); | ||
删除文件 | ||
======= | ||
|
||
可以调用 ``fs.unlink()`` 来删除指定的文件: | ||
|
||
.. code-block:: javascript | ||
// 删除"mynewfile2.txt" | ||
var fs = require('fs'); | ||
fs.unlink('mynewfile2.txt', function (err) { | ||
if (err) throw err; | ||
console.log('File deleted!'); | ||
}); | ||
重命名文件 | ||
========== | ||
|
||
可以调用 ``fs.rename()`` 方法来重命名文件: | ||
|
||
.. code-block:: javascript | ||
// 将 "mynewfile1.txt" 重命名为 "myrenamedfile.txt" | ||
var fs = require('fs'); | ||
fs.rename('mynewfile1.txt', 'myrenamedfile.txt', function (err) { | ||
if (err) throw err; | ||
console.log('File Renamed!'); | ||
}); | ||
下一步 | ||
====== | ||
|
||
.. include:: /_fragments/next-step-to-wechat-mp.rst | ||
|
||
.. include:: /_fragments/wechat-reward.rst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
.. 标准库 | ||
FileName: index.rst | ||
Author: Huang Xiaoyan | ||
Created: 2018-06-03 16:14:00 | ||
@contact: hxysnail@gmail.com | ||
@version: $Id$ | ||
Description: | ||
Changelog: | ||
========== | ||
HTTP模块 | ||
========== | ||
|
||
`Node.js` 有一个内置的HTTP模块,允许 `Node.js` 使用超文本传输协议(HTTP)传输数据。 | ||
|
||
我们使用 ``require`` 函数来引入 `HTTP` 模块。 | ||
|
||
.. code-block:: javascript | ||
var http = require('http') | ||
Node.js作为web服务器 | ||
=================== | ||
|
||
`HTTP` 模块可以创建一个 `HTTP` 服务器,监听服务器端口,并给客户端返回响应信息。 | ||
|
||
使用 ``createServer`` 方法来创建一个 `HTTP` 服务器: | ||
|
||
.. code-block:: javascript | ||
var http = require('http'); | ||
//create a server object: | ||
http.createServer(function (req, res) { | ||
res.write('Hello World!'); //write a response to the client | ||
res.end(); //end the response | ||
}).listen(8080); //the server object listens on port 8080 | ||
传递到 ``http.createServer()`` 中的回调函数,将会在访问 `8080` 端口时被执行。 | ||
|
||
把上述代码保存到一个命名为 ``demo_http.js`` 的文件中,并初始化该文件: | ||
|
||
.. code-block:: shell | ||
C:\Users\Your Name>node demo_http.js | ||
如果你遵循了上述步骤,访问 `<http://localhost:8080>`_ ,你将会看到和例子中一样的结果。 | ||
|
||
添加HTTP头部 | ||
=========== | ||
|
||
如果 `HTTP` 服务器返回的内容需要以 ``HTML`` 的格式展示,你需要在 `HTTP` 头部中包含一个正确的文本类型: | ||
|
||
.. code-block:: javascript | ||
var http = require('http'); | ||
http.createServer(function (req, res) { | ||
res.writeHead(200, {'Content-Type': 'text/html'}); | ||
res.write('Hello World!'); | ||
res.end(); | ||
}).listen(8080); | ||
``res.writeHead()`` 函数的第一个参数是状态码,``200`` 表示 ``ok`` 。第二个参数是一个包含响应头部的对象。 | ||
|
||
读取查询字符串 | ||
============ | ||
|
||
``http.createServer()`` 的回调函数中,有一个 ``req`` 参数,代表从客户端传来的请求对象(继承于 ``http.IncomingMessage`` 对象)。 | ||
|
||
这个对象有一个 ``url`` 的属性,保存着域名之后的 `url参数部分`。 | ||
|
||
.. code-block:: javascript | ||
// demo_http_url.js | ||
var http = require('http'); | ||
http.createServer(function (req, res) { | ||
res.writeHead(200, {'Content-Type': 'text/html'}); | ||
res.write(req.url); | ||
res.end(); | ||
}).listen(8080); | ||
把上述代码保存到一个名为 ``demo_http_url.js`` 的文件中,并初始化文件。 | ||
|
||
.. code-block:: shell | ||
C:\Users\Your Name>node demo_http_url.js | ||
如果你遵循了上述的步骤,访问以下两个地址,你将会得到两个不同的结果。 | ||
|
||
`<http://localhost:8080/summer>`_ | ||
|
||
会产生这个结果: ``/summer`` | ||
|
||
`<http://localhost:8080/winter>`_ | ||
|
||
会产生这个结果: ``/winter`` | ||
|
||
分割查询字符串 | ||
============= | ||
|
||
有一些内置模块可以很容易地把查询字符串分割为可读的结构,比如 `URL` 模块。 | ||
|
||
.. code-block:: javascript | ||
// 分割查询字符串为可读结构 | ||
var http = require('http'); | ||
var url = require('url'); | ||
http.createServer(function (req, res) { | ||
res.writeHead(200, {'Content-Type': 'text/html'}); | ||
var q = url.parse(req.url, true).query; | ||
var txt = q.year + " " + q.month; | ||
res.end(txt); | ||
}).listen(8080); | ||
把上述代码保存到一个名为``demo_querystring.js``的文件,并初始化文件。 | ||
|
||
.. code-block:: shell | ||
C:\Users\Your Name>node demo_querystring.js | ||
访问以下地址: | ||
|
||
`<http://localhost:8080/?year=2017&month=July>`_ | ||
|
||
会产生这个结果: ``2017 July`` | ||
|
||
下一步 | ||
====== | ||
|
||
.. include:: /_fragments/next-step-to-wechat-mp.rst | ||
|
||
.. include:: /_fragments/wechat-reward.rst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
.. 标准库 | ||
FileName: index.rst | ||
Author: Huang Xiaoyan | ||
Created: 2018-06-03 16:14:00 | ||
@contact: hxysnail@gmail.com | ||
@version: $Id$ | ||
Description: | ||
Changelog: | ||
=========== | ||
Node.js之旅 | ||
=========== | ||
|
||
.. toctree:: | ||
:maxdepth: 3 | ||
|
||
模块 <module> | ||
HTTP模块 <http> | ||
文件模块 <file-system> | ||
|
||
|
||
.. comments | ||
comment something out below | ||
Oops, something went wrong.