无法在nodejs中处理请求

时间:2018-02-02 09:34:58

标签: node.js express body-parser

在我的快递应用程序中,我在收到用户请求时遇到以下错误。如果请求正文是ib JSON格式,则没有问题

UnsupportedMediaTypeError: unsupported charset "ISO-8859-1"
    at urlencodedParser (/var/www/payment/node_modules/body-parser/lib/types/urlencoded.js:108:12)
    at Layer.handle [as handle_request] (/var/www/payment/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/var/www/payment/node_modules/express/lib/router/index.js:317:13)
    at /var/www/payment/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/var/www/payment/node_modules/express/lib/router/index.js:335:12)
    at next (/var/www/payment/node_modules/express/lib/router/index.js:275:10)
    at IncomingMessage.<anonymous> (/var/www/payment/gate.js:392:5)
    at IncomingMessage.emit (events.js:159:13)
    at endReadableNT (_stream_readable.js:1054:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11

我已经在我的代码中设置了这个

app.use(bodyParser.json());

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

1 个答案:

答案 0 :(得分:0)

body-parser docs说:

  

返回仅解析urlencoded主体的中间件,仅查看Content-Type标头与type选项匹配的请求。此解析器仅接受正文的UTF-8编码,并支持gzip和deflate编码的自动膨胀。

我会尝试禁用(在您的代码中注释掉)bodyParser.urlencoded解析器。