没有以帖子形式获取数据

时间:2022-02-19 15:58:12

i've been trying to get the data in a form with the POST method, but i don't get any, i've been reading a book to learn about expressjs.. this is my form:

我一直试图用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>

and this is my node code.

这是我的节点代码。

/*
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);
});

i get "undefined" in request.body, which i dont know why i get this error, i've tried with other methods like request.param, or request.query, but they get nothing, i've made sure the post is detected and its why it sends the message that's being detected, but i want to get the data in the form..

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

2 个解决方案

#1


1  

You need the now-separate body-parser middleware. Run npm install body-parser or add body-parser to your package.json and npm install it.

你需要现在独立的身体解析器中间件。运行npm install body-parser或者将body-parser添加到你的package.json中,然后npm安装它。

Add var bodyParser = require('body-parser'); to your module-loading, then after instantiating your app, add the middleware with app.use(bodyParser());. That will populate req.body for the form.

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

#2


0  

add in app.js:

在app.js中添加:

app.use(express.bodyParser());

As the comment indicated, it depends on the express version.

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

It works with 3.x as stated above.

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

For 4.0, you must manually include these frameworks that were previously included. See the note from vision media (overview tab):

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

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

These are NOT included to allow independent updates

这些不包括在内以允许独立更新

#1


1  

You need the now-separate body-parser middleware. Run npm install body-parser or add body-parser to your package.json and npm install it.

你需要现在独立的身体解析器中间件。运行npm install body-parser或者将body-parser添加到你的package.json中,然后npm安装它。

Add var bodyParser = require('body-parser'); to your module-loading, then after instantiating your app, add the middleware with app.use(bodyParser());. That will populate req.body for the form.

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

#2


0  

add in app.js:

在app.js中添加:

app.use(express.bodyParser());

As the comment indicated, it depends on the express version.

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

It works with 3.x as stated above.

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

For 4.0, you must manually include these frameworks that were previously included. See the note from vision media (overview tab):

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

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

These are NOT included to allow independent updates

这些不包括在内以允许独立更新