简体   繁体   English

Express JS中的正文解析器

[英]Body-parser in Express JS

I have been messing around with express js for a while now and I have come across something called body parser. 我已经和Express JS纠缠了一段时间了,遇到了一个叫做body parser的东西。 According to my research body parser allows us to POST content form our HTTP request. 根据我的研究机构的分析,解析器允许我们从HTTP请求中发布内容。

My question that, is it absolutely necessary to use body parser when using express.js? 我的问题是,在使用express.js时是否绝对需要使用正文解析器?

If it is not necessary then what are the advantages of using it and if body parser is not used then what needs to be done to make sure that the content is POSTED? 如果没有必要,那么使用它的好处是什么,如果不使用正文解析器,那么需要做什么以确保内容已发布?

Thank you in advance. 先感谢您。

Let's try to keep this least technical. 让我们尝试保持最低限度的技术性。

Let's say you are sending a html form data to node-js server ie you made a request to the server. 假设您正在将HTML表单数据发送到node-js服务器,即您向服务器发出了请求。 The server file would receive your request under a request object. 服务器文件将在请求对象下接收您的请求。 Now by logic, if you console log this request object in your server file you should see your form data some where in it, which could be extracted then, but whoa ! 现在按逻辑,如果您在服务器文件中控制台记录此请求对象,则应该在其中的某些位置看到表单数据,然后可以将其提取出来,但是哇! you actually don't ! 你实际上不!

So, where is our data ? 那么,我们的数据在哪里? How will we extract it if its not only present in my request. 如果它不仅出现在我的请求中,我们将如何提取它。

Simple explanation to this is http sends your form data in bits and pieces which are intended to get assembled as they reach their destination. 对此的简单解释是http将您的表单数据逐位发送,以便在到达目的地时进行组装。 So how would you extract your data. 那么您将如何提取数据。 Use something called “body-parser” which would do this for you. 使用一种叫做“ body-parser”的东西,它可以为您做到这一点。

body-parser parses your request and converts it into a format from which you can easily extract relevant information that you may need. body-parser解析您的请求并将其转换为一种格式,您可以从中轻松提取您可能需要的相关信息。 First of all require the following in your app.js file. 首先,在您的app.js文件中需要以下内容。

var bodyParser = require('body-parser')

and add the following line to make it work 并添加以下行使其起作用

app.use(bodyParser.urlencoded({extended: true}));

You can make use of the events on('data') and on('end') to extract the payload. 您可以利用on('data')on('end')来提取有效负载。 Whenever a POST request is sent, data arrives as a stream of bits. 无论何时发送POST请求,数据都会以比特流的形式到达。 When the stream comes in the on('data') event is fired, and you can start collecting the stream into a buffer. 当流进入时,将触发on('data')事件,您可以开始将流收集到缓冲区中。 When the stream ends (all the data has been received) the on('end') event is fired, that is when you can start using the data that you just collected. 当流结束(已接收到所有数据)时,将触发on('end')事件,即可以开始使用刚刚收集的数据时。

Yopu need to include stringDecoder (stringDecoder is a module that comes built into node) Yopu需要包括stringDecoder(stringDecoder是节点内置的模块)

const string_decoder= require('string_decoder').StringDecoder;

You need to have this piece of code running as middleware. 您需要使这段代码作为中间件运行。

var buffer= "";
request.on('data',  function(data_stream)
{
    //start collecting the data stream into a buffer
    buffer= buffer + utf8_decoder.write(data_stream);
});

request.on('end', function()
{
    buffer= buffer + utf8_decoder.end();
    //add the buffer to the request object so it can be accessed elsewhere
    request.payload= buffer;
});

This is probably the best way if you decide not to use any external libraries. 如果您决定不使用任何外部库,这可能是最好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM