如何从Express.js中的POST数据创建json对象?
我有以下表格:如何从Express.js中的POST数据创建json对象?,express,Express,我有以下表格: form(method="POST", action="/contato") .row .col-sm-4.form-group label NOME input(type="text",name="contato[nome]").form-control .col-sm-4.form-group label TELEFO
form(method="POST", action="/contato")
.row
.col-sm-4.form-group
label NOME
input(type="text",name="contato[nome]").form-control
.col-sm-4.form-group
label TELEFONE
input(type="text",name="contato[telefone]").form-control
.row
.col-sm-4.form-group.btn-group
input(type="submit", value="Salvar").btn.btn-success
a(href="/contato").btn.btn-primary Voltar
但在我看来:
router.post('/contato', (req, res) => {
console.log('body', req.body);
});
输出:
正文{'contato[nome]':'Rafael','contato[telefone]':'0000'}
但我想:
body { contato : { nome : 'Rafael', telefone : '0000' }}
这是怎么回事 添加
bodyParser
中间件可能有助于:
var bodyParser = require('body-parser');
....
....
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
确保两者都有
我收到了这个请求:
body{contato:{nome:'oleg',telefone:'123456'}}