无涯教程: Node.js - Web模块
2021/8/21 13:06:06
本文主要是介绍无涯教程: Node.js - Web模块,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Web服务器是一个软件应用程序,它处理HTTP客户端(例如Web浏览器)发送的HTTP请求,并返回网页以响应客户端, Web服务器通常提供html文档以及图像,样式表和脚本。
Web应用架构
Web应用程序通常分为四层-
Client - 该层由Web浏览器,移动浏览器或可以向Web服务器发出HTTP请求的应用程序组成。
Server - 此层具有Web服务器,可以拦截客户端发出的请求并将响应传递给他们。
Business Layer - 该层包含应用服务器,Web服务器利用该服务器来执行所需的处理。
Data Layer - 此层包含数据库或任何其他数据源。
创建Web服务器
Node.js提供了一个 http 模块,该模块可用于创建服务器的HTTP客户端,以下是侦听8081端口的HTTP服务器的最基本结构。
创建一个名为server.js的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文件。
<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/
向服务器发出请求
在任何浏览器中打开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文件-
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
验证输出。
<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.
这篇关于无涯教程: Node.js - Web模块的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-30React Native常用组件-点击组件
- 2024-05-30uniapp+vue3+uv-ui手机端后台OA管理模板
- 2024-05-29Python网络爬虫的时候json=就是让你少写个json.dumps()
- 2024-05-27React Native常用组件-展示组件
- 2024-05-27React Native常用组件-列表组件
- 2024-05-09vue3开发前端表单缓存自定义指令,移动端h5必备插件
- 2024-05-09React Hooks在class组件中的使用方式
- 2024-03-30[OIDC in Action] 2. 基于OIDC(OpenID Connect)的SSO(纯JS客户端)
- 2024-03-29terraform jsonencode
- 2024-03-13vuex-persist