在没有正文解析器的情况下,express 中的输入 JSON 在哪里?

Where is input JSON in express without body-parser?

我知道 body-parser 及其作用。我很想知道使用 express 时请求的数据在哪里。在 body-parser 解析输入之前存在哪种格式。

// parse urlencoded types to JSON
app.use(bodyParser.urlencoded({
    extended: true
}));

// parse various different custom JSON types as JSON
app.use(bodyParser.json({ type: 'application/*+json' }));

// parse some custom thing into a Buffer
app.use(bodyParser.raw({ type: 'application/vnd.custom-type' }));

// parse an HTML body into a string
app.use(bodyParser.text({ type: 'text/html' }));

如果使用其中的none,数据将在哪里?它将以哪种格式提供?

Node documentation.

中对此进行了相当详尽的介绍

The request object that's passed in to a handler implements the ReadableStream interface. This stream can be listened to or piped elsewhere just like any other stream. We can grab the data right out of the stream by listening to the stream's 'data' and 'end' events.

Express 实际上是在对 Node.js HTTP 服务器功能应用扩展,包括扩展本机 Request 和 Response 对象。因此,您同样可以像对待 native request object as well

一样对待 Express 请求

一个 POST 请求被发送到一个特定的路径(带有可选的查询参数)。请求的 body 是放置 POST 数据的地方。 Express默认读取请求的header,但不读取请求的body。 body-parser 中间件的工作是读取和解析该请求 body,以便您可以轻松获得其数据。

Where will be data if none of these is used? In which format will it be available?

因此,如果您没有安装 body-parser 中间件或者没有与数据格式相匹配的中间件版本,那么 body 仍然会在传入的请求流,等待读取。请求的 req 参数是一个可读流。数据将在该流中等待读取。

格式将是请求中 content-type header 所说的格式。对于经典形式post,它通常是application/x-www-form-urlencoded,但可以设置为其他类型,例如application/json。由请求者决定设置什么 content-type,然后他们必须根据该标准对 body 中的数据进行编码。

对于文件上传等内容,可能会使用其他内容类型,例如 multipart/form-data