【问题标题】:Expresss not receiving all query string parameters using curl使用 curl 表示不接收所有查询字符串参数
【发布时间】:2016-10-05 22:23:03
【问题描述】:

在 Express 4 中,我没有收到所有查询字符串参数,只有第一个。 Curl 显示了一些奇怪的输出,我不知道这是什么意思.. 我正在使用 cors 和 mongoose-pagination 模块。

这里发生了什么?

使用卷曲: 卷曲http://localhost:3000/products?_dc=1465158748756&page=1&start=0&limit=25

在 app.js 中:

//Enable CORS
app.use(cors());
app.options('*', cors());


app.use('/', routes);
app.use('/users', users);
app.use('/products', products);

在产品控制器中:

/* GET products listing. */
router.get('/', function(req, res) {
  var page = req.query.page ? 1 : req.query.page;
  //var start = req.query.start;
  var limit = req.query.limit ? 1 : req.query.limit;
  db.product.paginate({}, { page: page, limit: limit }, function(err, p){
     if(err){
        console.log(err);
        //Error
        res.status = 500;
        return res.send(new Error(err)); 
      }
      return res.send(p);
    });
});

req.query 只包含第一个参数:

req.query
Object
_dc: "1465158748756"
__proto__: Object

这是 curl 的输出,我不知道如何解释:

oscar@dlp-AOD255E:~/node/HomeInventoryServer$ curl http://localhost:3000/products?_dc=1465158748756&page=1&start=0&limit=25
[1] 3471
[2] 3472
[3] 3473

【问题讨论】:

    标签: node.js curl express


    【解决方案1】:

    好的,只是为了记录,看来我需要将 url 放在引号内 " 因为 curl 以不同的方式解释它。

    curl "http://localhost:3000/products?_dc=1465158748756&page=1&start=0&limit=25"
    

    【讨论】:

    • 还要检查这个: var page = req.query.page ? 1:req.query.page;这会将 page 设置为 1,即使您已设置 req.query.page。更好的方法是: var page = req.query.page || 1
    • @Joenesy 非常感谢您的建议!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多