简体   繁体   English

快递4:POST方法

[英]Express 4: POST method

I'm building an application using Express 4. I'm trying to get some data out of a form, but it always returns empty {} 我正在使用Express 4构建应用程序。我正在尝试从表单中获取一些数据,但是它总是返回空{}

I have the following lines in app.js : 我在app.js中有以下几行:

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

var signup = require('./routes/signup');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.use('/signup', signup);

My route handler, signup.js : 我的路由处理程序signup.js

var express = require('express');
var router = express.Router();

// signup routing handler
router.get('/', function (req, res) {
    res.render('signup');
});
router.post('/', function (req, res) {
    console.log(JSON.stringify(req.body));
    res.send('Foo Bar!');
});

module.exports = router;

My form, in Jade format: 我的表单,采用Jade格式:

form.form-horizontal(method='post')
    .form-group
        label.col-sm-2.control-label(for='firstName') 
            | First Name:
                .col-sm-10
                    input.form-control( #firstName, type='text', placeholder='John')
    .form-group
        .col-sm-2.control-label
            button.btn.btn-default(type='submit')
                        | Sign Up!

The inputs in the Jade file need names in order to have keys in the JSON Jade文件中的输入需要名称,以便在JSON中具有键

Like this: 像这样:

form.form-horizontal(method='post')
.form-group
    label.col-sm-2.control-label(for='firstName') 
        | First Name:
            .col-sm-10
                input.form-control( #firstName, 
                                    type='text', 
                                    placeholder='John', 
                                    name='firstName')
.form-group
    .col-sm-2.control-label
        button.btn.btn-default(type='submit')
                    | Sign Up!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM