Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

express post请求res.body 取不到数据 #10

Open
raclen opened this issue Sep 15, 2019 · 0 comments
Open

express post请求res.body 取不到数据 #10

raclen opened this issue Sep 15, 2019 · 0 comments
Labels
博客搬迁 以前博客的文章迁移过来

Comments

@raclen
Copy link
Owner

raclen commented Sep 15, 2019

使用post请求,json格式的时候,req.body=undefind 这里需要使用一个中间件body-parser

var express = require('express');
var bodyParser = require('body-parser');

var app = express();

// see https://github.com/expressjs/body-parser
// 添加 body-parser 中间件就可以了
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());

app.post('/', function (req, res) {
    console.log('req.body', req.body);
    res.send({airead: 'fan'});
});

app.listen(8888);

参考链接
https://cnodejs.org/topic/53c89067c9507b4044b1deaa
https://github.com/expressjs/body-parser

#2015-10-19

@raclen raclen added the 博客搬迁 以前博客的文章迁移过来 label Sep 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
博客搬迁 以前博客的文章迁移过来
Projects
None yet
Development

No branches or pull requests

1 participant