- Node.js 教程
- Node.js - 主页
- Node.js - 简介
- Node.js - 环境设置
- Node.js - 第一个应用程序
- Node.js - REPL 终端
- Node.js - 包管理器 (NPM)
- Node.js - 回调概念
- Node.js - 事件循环
- Node.js - 事件发射器
- Node.js - 缓冲区
- Node.js - 流
- Node.js - 文件系统
- Node.js - 全局对象
- Node.js - 实用模块
- Node.js - Web 模块
- Node.js - Express 框架
- Node.js - RESTFul API
- Node.js - 扩展应用程序
- Node.js - 打包
- Node.js - 内置模块
- Node.js 有用资源
- Node.js - 快速指南
- Node.js - 有用的资源
- Node.js - 讨论
Node.js - Web 模块
什么是网络服务器?
Web 服务器是一种软件应用程序,它处理 HTTP 客户端(如 Web 浏览器)发送的 HTTP 请求,并返回网页以响应客户端。Web 服务器通常提供 html 文档以及图像、样式表和脚本。
大多数Web服务器都支持服务器端脚本,使用脚本语言或将任务重定向到应用程序服务器,应用程序服务器从数据库检索数据并执行复杂的逻辑,然后通过Web服务器将结果发送到HTTP客户端。
Apache Web 服务器是最常用的 Web 服务器之一。它是一个开源项目。
网络应用架构
Web 应用程序通常分为四层 -
客户端- 该层由 Web 浏览器、移动浏览器或可以向 Web 服务器发出 HTTP 请求的应用程序组成。
服务器- 这一层有 Web 服务器,它可以拦截客户端发出的请求并将响应传递给它们。
业务- 该层包含 Web 服务器用来执行所需处理的应用程序服务器。该层通过数据库或者一些外部程序与数据层交互。
数据- 该层包含数据库或任何其他数据源。
使用 Node 创建 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 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/');
接下来,我们在创建 server.js 的同一目录中创建以下名为 index.htm 的 html 文件。
文件:index.htm
<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.htm,可以看到如下结果。
验证服务器端的输出。
Server running at http://127.0.0.1:8081/ Request for /index.htm received.
使用 Node 创建 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 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();
现在从除 server.js 之外的不同命令终端运行 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.htm received.
