没有以帖子形式获取数据

时间:2014-04-24 20:33:47

标签: node.js express

我一直在尝试使用POST方法获取表单中的数据,但我没有得到任何,我一直在读书以了解expressjs ..这是我的形式:

<form method="post">
    <div class="form-group">
        <label for="usernameInput" class="col-md-2">Username</label>
        <div class="col-md-12">
            <input type="text" class="form-control" id="usernameInput" name="username" placeholder="Enter username">
        </div>
    </div>

    <div class="form-group">
        <label for="passwordInput" class="col-md-2">Password</label>
        <div class="col-md-12">
            <input type="password" class="form-control" id="passwordInput" name="password" placeholder="Enter password">
        </div>
    </div>

    <div class="form-group">
        <div class="col-sm-offset-1 col-sm-11">
            <div class="checkbox">
                <label>
                    <input type="checkbox"> Remember me
                </label>
            </div>
        </div>
    </div>

    <button type="submit" class="btn btn-default">Log in</button>
</form>

这是我的节点代码。

/*
Chat application for @node.js
express version.
*/

//Load modules.
var express = require('express'),
    http = require('http'),
    socket = require('socket.io'),
    swig = require('swig'),
    fs = require('fs');

//Load config.
console.log('Loading configuration.');
var config = fs.readFileSync('config.json');
var config = JSON.parse(config);
var port = config.port;
var views = config.views;
console.log('Configuration loaded.');

//Initiate express module in app.
var app = express();


//Global vars
var Title = "Node.js Chat";
var result = '';


app.engine('html', swig.renderFile);

//Set view engine.
app.set('view engine', 'html');

//Set the directory for views.
app.set('views', __dirname + '/views');

swig.setDefaults(
{
    cache: false
});

app.get('/', function(request, response)
{
    console.log('GET OK');
    response.render('index',
    {
        'Title': Title,
        'result': result,
    });
});

app.post('/', function(request, response)
{
    console.log('POST OK');
    console.log(request.body);

    response.render('index',
    {
        'Title': Title,
        'result': 'Post detected.',
    });
});

//logger.
app.use(function(request, response, next)
{
    console.log('%s %s', request.method, request.url);

    var file = request.url.slice(1 + request.url.indexOf('/'));

    app.get(request.url, function(request, response)
    {
        response.render(file,
        {
            //Var to be named in the render : value;
            'Title': Title,
            'result': result,
        });
    });

    next();
});


//Set directory for static files (css, js, img)
app.use(express.static(__dirname + '/public'));

//Run the app.
http.createServer(app).listen(port, function()
{
    console.log('Server listening to ' + port);
});

我得到&#34;未定义&#34;在request.body,我不知道为什么我得到这个错误,我已尝试过其他方法,如request.param或request.query,但他们什么都没得到,我确保检测到帖子以及为什么它发送被检测到的消息,但我想在表格中获取数据..

2 个答案:

答案 0 :(得分:1)

您需要现在单独的body-parser middleware。运行npm install body-parser或将body-parser添加到您的package.json和npm install

var bodyParser = require('body-parser');添加到模块加载中,然后在实例化app后,使用app.use(bodyParser());添加中间件。这将为表单填充req.body

答案 1 :(得分:0)

在app.js中添加:

app.use(express.bodyParser());

如评论所示,这取决于快递版本。

如上所述,它适用于3.x.

对于4.0,您必须手动包含以前包含的这些框架。请参阅视觉媒体(概述标签)中的注释:

https://github.com/visionmedia/express/wiki/Migrating-from-3.x-to-4.x

不包含这些内容以允许独立更新