3

实战Node.js之GET/POST请求在Web 应用架构在客户端的使用

 1 year ago
source link: https://blog.51cto.com/u_15603561/5591698
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

实战Node.js之GET/POST请求在Web 应用架构在客户端的使用

推荐 原创

Table of Contents

GET/POST请求

实战Node.js之GET/POST请求在Web 应用架构在客户端的使用_服务器

在许多情况下,我们的服务器需要处理用户的浏览器,例如表单提交。
get/post请求通常用于向服务器提交表单。

var http = require('http');
var url = require('url');
var util = require('util');
 
http.createServer(function(req, res){
    res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
    res.end(util.inspect(url.parse(req.url, true)));
}).listen(3000);

在浏览器中访问http://localhost:3000/user?name=黎燃&url=并查看返回的结果:

实战Node.js之GET/POST请求在Web 应用架构在客户端的使用_javascript_02
我们可以使用URL解析方法来解析URL中的参数。代码如下:
var http = require('http');
var url = require('url');
var util = require('util');
 
http.createServer(function(req, res){
    res.writeHead(200, {'Content-Type': 'text/plain'});

解析 url 参数

    var params = url.parse(req.url, true).query;
    res.write("网站名:" + params.name);
    res.write("\n");
    res.write("网站 URL:" + params.url);
    res.end();

获取 POST 请求内容

post请求的内容都在请求正文中,http Serverrequest没有属性content作为请求正文,因为等待发送请求正文可能是一项耗时的任务。
例如,在上传文件时,我们可能不需要注意请求主体的内容。
恶意post请求将极大地消耗服务器的资源,因此node JS默认情况下不会解析请求体。

var http = require('http');
var querystring = require('querystring');
var util = require('util');

定义了一个post变量,用于暂存请求体的信息

var post = ''; 

通过req的data事件监听函数,每当接受到请求体的数据,就累加到post变量中

req.on('data', function(chunk){    
        post += chunk;
    });

在end事件触发后,通过querystring.parse将post解析为真正的POST请求格式,然后向客户端返回。

 req.on('end', function(){    
        post = querystring.parse(post);
        res.end(util.inspect(post));
    });

Web 应用架构

Web服务器通常指的是Web服务器,它指的是驻留在Internet上的特定类型计算机的程序。
web服务器的基本功能是提供web信息浏览服务。它只需要支持HTTP协议、HTML文档格式和URL,并与客户端的web浏览器协作。
大多数web服务器支持服务器端脚本语言(PHP、python、Ruby)等,并通过脚本语言从数据库获取数据,然后将结果返回到客户端浏览器。
目前,最流行的三种web服务器是Apache、nginx和IIS。

实战Node.js之GET/POST请求在Web 应用架构在客户端的使用_客户端_03
节点。JS提供了一个HTTP模块。HTTP模块主要用于构建HTTP服务器和客户端。使用HTTP服务器或客户端函数时必须调用HTTP模块。代码如下:
var http = require('http');
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 状态码: 404 : NOT FOUND,Content Type: text/html

response.writeHead(404, {'Content-Type': 'text/html'});
      }else{  

响应文件内容

 response.write(data.toString()); 

发送响应数据

response.end();

控制台会输出以下信息

console.log('Server running at http://127.0.0.1:8080/');

使用 Node 创建 Web 客户端

Node需要引入HTTP模块来创建web客户端和客户端JS文件

var http = require('http');

用于请求的选项

var options = {
   host: 'localhost',
   port: '8080',
   path: '/index.html'  
};

处理响应的回调函数

var callback = function(response){

不断更新数据

 var body = '';
   response.on('data', function(data) {
      body += data;
   });

数据接收完成

console.log(body);

向服务端发送请求

var req = http.request(options, callback);
req.end();
  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK