什么是Web服务器?

Web服务器是处理由HTTP客户端发送的,如web浏览器的HTTP请求的软件应用程序,并返回响应于客户端网页. Web服务器通常伴随着图片,样式表和脚本的HTML文档。

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

Apache web服务器是最常用的网络服务器中的一个。它是一个开源项目。

Web应用程序体系结构

Web应用程序通常分为四个层次:

  • Client - 此层由Web浏览器,移动浏览器或应用程序,可以使HTTP请求到万维网服务器。

  • Server - 这一层包括可拦截的要求由客户通过Web服务器响应。

  • Business - 此层利用Web服务器执行所需的处理的应用程序服务器。这层交互以经由数据的基础上或一些外部程序数据层。

  • Data - 此层由数据库或任何的数据来源。

使用Node创建Web服务器

Node.js提供了可以用于创建任何HTTP服务器的客户端的HTTP模块。以下是HTTP服务器的最低限度的结构,它会在8081端口侦听。

创建一个js文件名为server.js:

File: server.js

var http = require('http');
var fs = require('fs');
var url = require('url');

// Create a server
http.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 message
console.log('Server running at http://127.0.0.1:8081/');

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

File: index.html

<html>
<head>
<title>Sample Page</title>
</head>
<body>
Hello World!
</body>
</html>

现在让我们运行server.js看到的结果:

$ node server.js

验证输出

Server running at http://127.0.0.1:8081/

请求Node.js服务器

在任何浏览器中打开http://127.0.0.1:8081/index.html,看看下面的结果。

验证服务器端输出。

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

使用Node创建Web客户端

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

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

File: client.js

var http = require('http');

// Options to be used by request
var options = {
   host: 'localhost',
   port: '8081',
   path: '/index.html'
};

// Callback function is used to deal with response
var 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 server
var req = http.request(options, callback);
req.end();

现在运行不同的命令终端运行client.js看到结果

$ node client.js

验证输出。

<html>
<head>
<title>Sample Page</title>
</head>
<body>
Hello World!
</body>
</html>

验证服务器端输出。

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

标签:Node    js    Web模块

本站文章除注明转载外,均为本站原创或编译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动共创优秀实例教程
转载请注明:文章转载自:易百教程 [http:/www.yiibai.com]
本文标题:Node.js Web模块
本文地址:http://www.yiibai.com/nodejs/nodejs_web_module.html

Node.js Web模块的更多相关文章

  1. Node.js Web 模块

    什么是 Web 服务器? Web服务器一般指网站服务器,是指驻留于因特网上某种类型计算机的程序,Web服务器的基本功能就是提供Web信息浏览服务.它只需支持HTTP协议.HTML文档格式及URL,与客 ...

  2. 40.Node.js Web 模块

    转自:http://www.runoob.com/nodejs/nodejs-module-system.html 什么是 Web 服务器? Web服务器一般指网站服务器,是指驻留于因特网上某种类型计 ...

  3. 利用OpenShift托管Node.js Web服务进行微信公众号开发

    最近写了一个微信的翻译机器人.用户只要关注该公众号,发送英文的消息,就能收到中文翻译的回复.有兴趣的读者可以扫描下面的二维码关注该公众号,尝试发送英文单词试试看.(有时候第一次发送单词会收到“该公众号 ...

  4. Node.js Web 开发框架大全《中间件篇》

    这篇文章与大家分享优秀的 Node.js 中间件模块.Node 是一个服务器端 JavaScript 解释器,它将改变服务器应该如何工作的概念.它的目标是帮助程序员构建高度可伸缩的应用程序,编写能够处 ...

  5. node.js Web应用框架Express.js(一)

    什么是Express.js Express 是一个简洁而灵活的 node.js Web应用框架, 提供一系列强大特性帮助你创建各种Web应用,提供丰富的HTTP工具以及来自Connect框架的中间件随 ...

  6. KoaHub.js是基于 Koa.js 平台的 Node.js web 快速开发框架

    koahubjs KoaHub.js -- 基于 Koa.js 平台的 Node.js web 快速开发框架.可以直接在项目里使用 ES6/7(Generator Function, Class, A ...

  7. Node.js Path 模块

    Node.js path 模块提供了一些用于处理文件路径的小工具,我们可以通过以下方式引入该模块: var path = require("path") 方法 序号 方法 & ...

  8. node.js Web应用框架Express入门指南

    node.js Web应用框架Express入门指南 作者: 字体:[增加 减小] 类型:转载 时间:2014-05-28 我要评论 这篇文章主要介绍了node.js Web应用框架Express入门 ...

  9. 37.Node.js工具模块---处理和转换文件路径的工具 Path模块

    转自:http://www.runoob.com/nodejs/nodejs-module-system.html Node.js path 模块提供了一些用于处理文件路径的小工具,我们可以通过以下方 ...

随机推荐

  1. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(33)-数据验证共享

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(33)-数据验证共享 注:本节阅读需要有MVC 自定义验证的基础,否则比较吃力 一直以来表单的验证都是不可 ...

  2. Qt之XML(一) DOM

      Qt之XML(一) 文档名称 Qt之XML 创建时间 2012-10-10 修改时间 2012-10-10 创建人 Baifx 简介(收获) 最近开始使用QtXml,学习了一番,写了几个小测试程序 ...

  3. spring servlet 扩展undertow

    官方地址:http://undertow.io/documentation/servlet/servlet-extensions.html  留待学习中,mark一下 源码地址:https://git ...

  4. 亲测安装php

    亲测安装php1.tar zvxf php-5.3.8.tar.gz 2.cd php-5.3.83../configure \ --prefix=/usr/local/php \--with-mys ...

  5. 关于C++的疑问剖析

    1)众所周知,抽象类是不存在对象的,只提供接口而不提供实现.但是抽象类能不能作为一个类指针,指向其子类的对象呢? class Interface { public: ; }; class Implem ...

  6. 安装Visual Studio 2013 中文社区版

    Visual Studio 2013 免费了,我收到邮件后,立即从邮件的下载连接安装了 Visual Studio Community 2013 with Update 4 . 安装后几天没打开,今天 ...

  7. PHP中的function函数详解

    PHP函数,在PHP中函数起到一个不可分割的重要部分,很多功能实现都要用到函数,PHP的最大的威力就来源于函数! 在PHP中内建函数至少有上千个函数.这些内建函数了解就行了,官方文档里面有函数大全:传 ...

  8. 当升级新版本的时候,从新加载新版本的js的方法

    <script src="../Script/SmcScript.js?version='<%=Smc20.Web.WebForm.Public.WebConst.WEBJSCA ...

  9. html 新元素

    html5新元素 html5语义元素 <header> 定义了文档或者文档的一部分区域的页眉 <nav> 定义了导航链接的部分 <section> 定义了文档的某个 ...

  10. windows Server 2008 -必须使用“角色管理工具”安装或配置Microsoft .Net Framework 3.5

    在windows Server 2008上安装 .Net Framework 3.5的时候,报错:必须使用“角色管理工具”安装或配置Microsoft .Net Framework 3.5. Solu ...