- 第1节:Node.js 教程
- 第2节:Node.js 安装配置
- 第3节:Node.js 创建第一个应用
- 第4节:Node.js NPM 使用介绍
- 第5节:Node.js REPL(交互式解释器)
- 第6节:Node.js 回调函数
- 第7节:Node.js 事件循环
- 第8节:Node.js EventEmitter
- 第9节:Node.js Buffer(缓冲区)
- 第10节:Node.js Stream(流)
- 第11节:Node.js 模块系统
- 第12节:Node.js 函数
- 第13节:Node.js 路由
- 第14节:Node.js 全局对象
- 第15节:Node.js 常用工具util
- 第16节:Node.js 文件系统
- 第17节:Node.js GET/POST请求
- 第18节:Node.js 工具模块
- 第19节:Node.js Web 模块
- 第20节:Node.js Express 框架
- 第21节:Node.js RESTful API
- 第22节:Node.js 多进程
- 第23节:Node.js JXcore 打包
- 第24节:Node.js 连接 MySQL
- 第25节:Node.js 连接 MongoDB
- 第26节:关于 Node.js
- 第27节:Node.js 示例
- 第28节:Node.js 断言测试
- 第29节:Node.js 事件
- 第30节:Node.js Punycode
- 第31节:Node.js Buffer
- 第32节:Node.js 文件系统
- 第33节:Node.js Query Strings
- 第34节:Node.js C/C++ 插件
- 第35节:Node.js 逐行读取
- 第36节:Node.js 全局对象
- 第37节:Node.js 子进程
- 第38节:Node.js REPL
- 第39节:Node.js HTTP
- 第40节:Node.js 集群
- 第41节:Node.js Smalloc
- 第42节:Node.js HTTPS
- 第43节:Node.js 控制台
- 第44节:Node.js 模块
- 第45节:Node.js 加密
- 第46节:Node.js 流
- 第47节:Node.js 网络
- 第48节:Node.js 调试器
- 第49节:Node.js 字符串解码器
- 第50节:Node.js 系统
- 第51节:Node.js DNS
- 第52节:Node.js 定时器
- 第53节:Node.js 路径
- 第54节:Node.js 域
- 第55节:Node.js TLS/SSL
- 第56节:Node.js 进程
- 第57节:Node.js TTY
- 第58节:Node.js UDP/Datagram
- 第59节:Node.js URL
- 第60节:Node.js 实用工具
- 第61节:Node.js 虚拟机
- 第62节:Node.js ZLIB
Node.js 路由
我们要为路由提供请求的URL和其他需要的GET及POST参数,随后路由需要根据这些数据来执行相应的代码。
因此,我们需要查看HTTP请求,从中提取出请求的URL以及GET/POST参数。这一功能应当属于路由还是服务器(甚至作为一个模块自身的功能)确实值得探讨,但这里暂定其为我们的HTTP服务器的功能。
我们需要的所有数据都会包含在request对象中,该对象作为onRequest()回调函数的第一个参数传递。但是为了解析这些数据,我们需要额外的Node.JS模块,它们分别是url和querystring模块。
url.parse(string).query
|
url.parse(string).pathname |
| |
| |
------ -------------------
http://localhost:8888/start?foo=bar&hello=world
--- -----
| |
| |
querystring(string)["foo"] |
|
querystring(string)["hello"]
当然我们也可以用querystring模块来解析POST请求体中的参数,稍后会有演示。
现在我们来给onRequest()函数加上一些逻辑,用来找出浏览器请求的URL路径:
var http = require("http");
var url = require("url");
function start() {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;
好了,我们的应用现在可以通过请求的URL路径来区别不同请求了--这使我们得以使用路由(还未完成)来将请求以URL路径为基准映射到处理程序上。
在我们所要构建的应用中,这意味着来自/start和/upload的请求可以使用不同的代码来处理。稍后我们将看到这些内容是如何整合到一起的。
现在我们可以来编写路由了,建立一个名为router.js的文件,添加以下内容:
function route(pathname) {
console.log("About to route a request for " + pathname);
}
exports.route = route;
如你所见,这段代码什么也没干,不过对于现在来说这是应该的。在添加更多的逻辑以前,我们先来看看如何把路由和服务器整合起来。
我们的服务器应当知道路由的存在并加以有效利用。我们当然可以通过硬编码的方式将这一依赖项绑定到服务器上,但是其它语言的编程经验告诉我们这会是一件非常痛苦的事,因此我们将使用依赖注入的方式较松散地添加路由模块。
首先,我们来扩展一下服务器的 start() 函数,以便将路由函数作为参数传递过去,server.js 文件代码如下
var http = require("http");
var url = require("url");
function start(route) {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
route(pathname);
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;
同时,我们会相应扩展index.js,使得路由函数可以被注入到服务器中:
var server = require("./server");
var router = require("./router");
server.start(router.route);
在这里,我们传递的函数依旧什么也没做。
如果现在启动应用(node index.js,始终记得这个命令行),随后请求一个URL,你将会看到应用输出相应的信息,这表明我们的HTTP服务器已经在使用路由模块了,并会将请求的路径传递给路由:
bash$ node index.js
Request for /foo received.
About to route a request for /foo
以上输出已经去掉了比较烦人的/favicon.ico请求相关的部分。
浏览器访问 http://127.0.0.1:8888/,输出结果如下: