【问题标题】:Sails.js body contents when Content-Type is text/csv当 Content-Type 为 text/csv 时 Sails.js 正文内容
【发布时间】:2015-07-18 00:40:39
【问题描述】:

我是否需要做任何特别的事情来处理带有文本 Content-TypePOST 请求?

我需要处理text/csv,但是当我访问控制器中的方法时,看起来 Sails.js 试图将正文解析为 JSON:

postStuff: function(req, res) {
    sails.log.info("postStuff")
    sails.log.info(req.body)
    sails.log.info(req.headers['content-type']);
...etc...

给我:

info: postStuff
info: {}
info: text/csv

我发现bodyParser 中间件的文档有点晦涩。

FWIW,我也尝试在请求中将Content-Type 设置为text/plain,但无济于事。

我也尝试显式添加一个文本bodyParser作为中间件,似乎没有任何效果:

http.js

module.exports.http = {
  bodyParserText: require('body-parser').text(),
  middleware: {
    order: [
      'startRequestTimer',
      'cookieParser',
      'session',
      'myRequestLogger',
      'bodyParser',
      'bodyParserText',
      'handleBodyParserError',
      'compress',
      'methodOverride',
      'poweredBy',
      '$custom',
      'router',
      'www',
      'favicon',
      '404',
      '500'
    ],
...etc...

【问题讨论】:

  • 请注意bodyParserTextmiddleware.order 列表中的排序也没有任何区别。

标签: node.js express sails.js


【解决方案1】:

哇,原来这很容易……但由于缺乏来自框架的反馈,调试起来非常麻烦。

https.js 中,新中间件的元素必须在middleware 元素内...如果当然有意义的话:

module.exports.http = {
  // NOT HERE
  //  bodyParserText: require('body-parser').text(),
  middleware: {
    // HERE
    bodyParserText: require('body-parser').text(),
    order: [
      'startRequestTimer',
      'cookieParser',
      'session',
      'myRequestLogger',
      'bodyParser',
      'bodyParserText',
      'handleBodyParserError',
...etc...

如果sails.js(快递?)发出警告会很好,因为我假设它找不到bodyParserText

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-27
    • 1970-01-01
    • 1970-01-01
    • 2015-03-14
    • 2019-05-01
    • 2013-11-21
    • 2010-12-18
    • 2011-12-25
    相关资源
    最近更新 更多