开发手册 欢迎您!
软件开发者资料库

Node.js - Web模块

Node.js Web模块 - 从简单和简单的步骤学习Node.js框架,从基本到高级概念,包括简介,环境设置,第一应用程序,REPL终端,节点包管理器,节点回调概念,事件发射器,节点缓冲器模块,节点流,节点文件系统,全局对象,节点实用程序模块,节点Web模块,节点快速应用程序,节点RESTFul API,节点扩展应用程序,打包。

什么是Web服务器?

Web服务器是一个软件应用程序,它处理HTTP客户端发送的HTTP请求,如Web浏览器,并返回网页以响应客户端. Web服务器通常提供html文档以及图像,样式表和脚本.

大多数Web服务器支持服务器端脚本,使用脚本语言或将任务重定向到应用程序服务器.从数据库中检索数据并执行复杂的逻辑,然后通过Web服务器将结果发送到HTTP客户端.

Apache Web服务器是最常用的Web服务器之一.它是一个开源项目.

Web应用程序架构

Web应用程序通常分为四层和零下;

Web Architecture

  • 客户 : 该层由可以向Web服务器发出HTTP请求的Web浏览器,移动浏览器或应用程序组成.

  • 服务器 : 该层具有Web服务器,可以拦截客户端发出的请求并将响应传递给他们.

  • 业务 : 该层包含Web服务器用于执行所需处理的应用程序服务器.该层通过数据库或一些外部程序与数据层交互.

  • 数据 : 该层包含数据库或任何其他数据源.

使用节点创建Web服务器

Node.js提供了一个 http 模块,可用于创建服务器的HTTP客户端.以下是在8081端口侦听的HTTP服务器的最小结构.

创建名为server.js : 的js文件;

文件:server.js

var http = require('http');var fs = require('fs');var url = require('url');// Create a serverhttp.createServer( function (request, response) {     // Parse the request containing file name   var pathname = url.parse(request.url).pathname;      // Print the name of the file for which request is made.   console.log("Request for " + pathname + " received.");      // Read the requested file content from file system   fs.readFile(pathname.substr(1), function (err, data) {      if (err) {         console.log(err);                  // HTTP Status: 404 : NOT FOUND         // Content Type: text/plain         response.writeHead(404, {'Content-Type': 'text/html'});      } else {         //Page found           // HTTP Status: 200 : OK         // Content Type: text/plain         response.writeHead(200, {'Content-Type': 'text/html'});                  // Write the content of the file to response body         response.write(data.toString());      }            // Send the response body       response.end();   });   }).listen(8081);// Console will print the messageconsole.log('Server running at http://127.0.0.1:8081/');

接下来让我们在您创建server.js的同一目录中创建以下名为index.htm的html文件.

文件:index.htm

         Sample Page               Hello World!   

现在让我们运行server.js来查看结果 :

  $ node server.js

验证输出.

Server running at http://127.0.0.1:8081/

向Node.js服务器发出请求

在任何浏览器中打开http://127.0.0.1:8081/index.htm以查看以下结果.

第一个服务器应用程序

验证服务器端的输出.

Server running at http://127.0.0.1:8081/Request for /index.htm received.

使用节点创建Web客户端

可以使用 http 模块创建Web客户端.让我们看看下面的例子.

创建一个名为client.js : 的js文件;

文件:client.js

var http = require('http');// Options to be used by request var options = {   host: 'localhost',   port: '8081',   path: '/index.htm'  };// Callback function is used to deal with responsevar callback = function(response) {   // Continuously update stream with data   var body = '';   response.on('data', function(data) {      body += data;   });      response.on('end', function() {      // Data received completely.      console.log(body);   });}// Make a request to the servervar req = http.request(options, callback);req.end();

现在从server.js以外的其他命令终端运行client.js以查看结果 :

  $ node client.js

验证输出.

         Sample Page               Hello World!   

验证服务器端的输出.

Server running at http://127.0.0.1:8081/Request for /index.htm received.