V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
0x001
V2EX  ›  Node.js

Express 4.2.0 post 为何不能解析参数呢。

  •  
  •   0x001 · Jun 28, 2014 · 6640 views
    This topic created in 4326 days ago, the information mentioned may be changed or developed.
    router.post('/', function(req, res) {
    console.log(req.body);
    console.log(req.body.name);
    console.log(req.body.tel);
    res.send("ok");
    });


    下面是log:
    {}
    undefined
    undefined
    POST / 200 2ms - 2b
    Supplement 1  ·  Jun 30, 2014
    找到原因了,因为我post的是一个raw的字符串,Express默认只支持json与x-www-form-urlencoded.
    7 replies    2014-06-30 10:30:50 +08:00
    coolicer
        1
    coolicer  
       Jun 28, 2014
    你加了body-parser没有
    coolicer
        2
    coolicer  
       Jun 28, 2014
    83053187 新手群欢迎你。
    0x001
        3
    0x001  
    OP
       Jun 28, 2014
    @coolicer 我用express创建的工程,加了body-parser的
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded());
    WildCat
        4
    WildCat  
       Jun 28, 2014 via iPhone
    试试
    req.param('name')
    breeswish
        5
    breeswish  
       Jun 28, 2014
    req.body: body
    req.param: query string
    RaymondYip
        6
    RaymondYip  
       Jun 29, 2014 via iPad
    Req.query
    acecode
        7
    acecode  
       Jun 30, 2014
    要不console.log( util.inspect(req, 3, 4, true) )看看?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2478 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 11:53 · PVG 19:53 · LAX 04:53 · JFK 07:53
    ♥ Do have faith in what you're doing.