Node.js Web模块
本篇主要介绍Node.js Web模块。学习Node.js web模块前,你首先应该先了解什么是Web服务器?
什么是 Web 服务器?
Web服务器一般指网站服务器,是指驻留于因特网上某种类型计算机的程序。
Web服务器的基本就是提供Web信息浏览服务。它只需HTTP协议、HTML文档格式及URL,与客户端的网络浏览器配合。
大多数web服务器都服务端的脚本语言(、python、ruby)等,并通过脚本语言从数据,将结果返回给客户端浏览器。
目前最主流的三个Web服务器是Apache、、IIS。
Web 应用架构
Client - 客户端,一般指浏览器,浏览器可以通过HTTP协议向服务器请求数据。
Server - 服务端,一般指Web服务器,可以接收客户端请求,并向客户端发送响应数据。
Business - 业务层, 通过Web服务器处理应用程序,如与交互,逻辑运算,外部程序等。
Data - 数据层,一般由组成。
使用 Node 创建 Web 服务器
Node.js提供了http模块,http模块主要用于搭建HTTP服务端和客户端,如果要使用HTTP服务器或客户端,则必须http模块,如下:
var http = require('http');
以下是演示最基本的HTTP服务器架构(使用8081端口),创建server.js,如下所示:
var http = require('http'); var fs = require('fs'); var url = require('url'); // 创建服务器 http.createServer( function (request, response) { // 解析请求,名 var pathname = url.parse(request.url).pathname; // 请求的名 console.log("Request for " + pathname + " received."); // 从系统中读取请求的 fs.readFile(pathname.substr(1), function (err, data) { if (err) { console.log(err); // HTTP 状态码: : NOT FOUND // Content Type: text/plain response.writeHead(, {'Content-Type': 'text/html'}); }else{ // HTTP 状态码: 200 : OK // Content Type: text/plain response.writeHead(200, {'Content-Type': 'text/html'}); // 响应 response.write(data.toString()); } // 发送响应数据 response.end(); }); }).listen(8081); // 控制台会以下信息 console.log('Server running at http://127.0.0.1:8081/');
接下来我们在该目录下创建index.htm,如下:
<html> <head> <title>前端之家 F2er.com</title> </head> <body> Hello F2er.com! </body> </html>
执行server.js:
$ node server.js Server running at http://127.0.0.1:8081/
接着我们在浏览器中输入并打开地址:http://127.0.0.1:8081/index.htm,如下图所示:
执行server.js的控制台信息如下:
Server running at http://127.0.0.1:8081/ Request for /index.htm received. # 客户端请求信息
使用 Node 创建 Web 客户端
使用Node创建Web客户端需要引入http模块,创建client.js,如下所示:
var http = require('http'); // 用于请求的选项 var options = { host: 'localhost', port: '8081', path: '/index.htm' }; // 处理响应的回调 var callback = function(response){ // 不断更新数据 var body = ''; response.on('data', function(data) { body += data; }); response.on('end', function() { // 数据接收完成 console.log(body); }); } // 向服务端发送请求 var req = http.request(options, callback); req.end();
新开终端,执行client.js,结果如下:
$ node client.js <html> <head> <title>前端之家 F2er.com</title> </head> <body> Hello F2er.com! </body> </html>
执行server.js的控制台信息如下:
Server running at http://127.0.0.1:8081/ Request for /index.htm received. # 客户端请求信息