表达4.9 bodyparser req.body.name为空

时间:2016-02-10 10:16:36

标签: node.js express body-parser

我正在使用express 4.9和nodemailer实现联系表单,但我似乎无法捕获req.body。带有编码值的电子邮件工作正常。我读到了关于使用bodyParser的内容,所以我添加了它(我认为之前的)路由。它仍然没有拿起req.body。可能是我在表格中遗漏了什么?

任何帮助都将不胜感激。

干杯,迈克

我的设置如下:

我的app.js

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

var app = express();

// view engine setup
app.engine('html', swig.renderFile);
app.set('views', path.join(__dirname, 'app_server', 'views'));  
app.set('view engine', 'html');

app.use(logger('dev'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

var routes = require('./app_server/routes/index');
// routes for the different pages
app.use('/', routes);

我的路线; index.js

var express = require('express');
var router = express.Router();
var ctrl_contact = require('../controllers/contact');

router.post('/contact', ctrl_contact.mail);

module.exports = router;

和控制器contact.js

// function to send email from contact form
module.exports.mail = function (req, res) {

    console.log('testing');
    console.log(req.body.name);
    console.log(req.body.email);
    console.log(req.body.subject);

    // etc BUT HERE req.body is already {}
}

我的表单看起来像

           <form name="sentMessage" id="contactForm" method="post" novalidate>
                <div class="row">
                    <div class="col-md-6">
                        <div class="row control-group">
                            <div class="form-group col-xs-12 controls">
                                <label>Name<span>*</span></label>
                                <input type="text" class="form-control" placeholder="Name" id="name" required data-validation-required-message="Please enter your name.">
                                <p class="help-block"></p>
                            </div>
                        </div>

                    </div>

                <div class="row control-group">
                    <div class="form-group col-xs-12  controls">
                        <label>Subject<span>*</span></label>
                        <input type="text" class="form-control" placeholder="Subject" id="subject" required data-validation-required-message="Subject.">
                        <p class="help-block"></p>
                    </div>
                </div>
                <div class="row control-group">
                    <div class="form-group col-xs-12 controls">
                        <label>Message<span>*</span></label>
                        <textarea rows="5" class="form-control" placeholder="Message" id="message" required data-validation-required-message="Please enter a message."></textarea>
                        <p class="help-block"></p>
                    </div>
                </div>
                <br>
                <div id="success"></div>
                <div class="row">
                    <div class="form-group col-xs-12">
                        <button type="submit" class="btn btn-theme-bg btn-lg">Send Message</button>
                    </div>
                </div>
            </form>

1 个答案:

答案 0 :(得分:0)

表单输入需要name属性才能获取bodyparser。

<input type="text" class="form-control" placeholder="Name" name="name"id="name" required data-validation-required-message="Please enter your name.">