简体   繁体   English

传递表单数据在Express / Node.js中不起作用

[英]Passing form data not working in Express / Node.js

Sorry if this is a remedial question, or an obvious error, but this is my first Node project. 抱歉,如果这是一个补救性问题或明显的错误,但这是我的第一个Node项目。

Essencailly what I need to do is get a line of text that a user will input into a form on the front end. Essencailly我需要做的是获取一行文本,用户将其输入到前端的表单中。 Unfortunately no matter what I do the result logs 'undefined'. 不幸的是,无论我做什么,结果日志都为“未定义”。 I'm at a loss. 我很茫然。 I'll post the full code below, but the section that giving me trouble is: 我将在下面发布完整的代码,但给我带来麻烦的部分是:

app.get('/test', function(req, res){
    res.render('test');
});

app.post('/test',function(req,res){
    console.log(req.body); 
});

the '/test' route is just temporary to test getting the form response (if for some reason that wasn't obvious). '/ test'路由只是临时的测试,以测试是否获得表单响应(如果出于某些原因而并不明显)。

My full HTML file looks like this: 我的完整HTML文件如下所示:

<!DOCTYPE HTML>
<head></head>
<body>
    <form id="myform" action="/test" method="post" enctype="application/x-www-form-urlencoded">
        <input type="text" name="name" id="mytext" />
        <input type="submit" id="mysubmit" />
    </form>
</body>

Any thoughts / help would be greatly appreciated. 任何想法/帮助将不胜感激。

Here is the full app.js file 这是完整的app.js文件

var app = require('express')();
var http = require('http').Server(app);
var exphbs  = require('express-handlebars');
var twitter = require('./twitter.js');
var hue = require("./hue_control.js");

//Variable that control Twitter Calls
var api = 'statuses/filter';
var params = {slug:'test', owner_screen_name: 'REDACTED', skip_status: true};
var values = {};

app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');

hue.activate();
twitter.tweetStream(api, params, values);

app.get('/test', function(req, res){
    res.render('test');
});

app.post('/test',function(req,res){
    console.log(req.body); //you will get your data in this as object.
    console.log(req.is('json'));
});

app.get('/', function(req, res){
    res.render('tweets');
});

app.get('/tweets', function(req, res){
    res.render('home', {   
        profileImg: values.profileImg,
        userName: values.userName,
        screenName: values.screenName,
        text: values.text,
        timeStamp: values.timeStamp,        
        tweetScore: values.tweetScore
    });
});



//app.get('/tweets', function(req, res)

http.listen(3000, function(){
    console.log('listening on *:3000');
});

I think you are missing the bodyParser middleware in your express application which is why req.body is undefined. 我认为您在快速应用程序中缺少bodyParser中间件,这就是为什么req.body未定义的原因。

If you are using Express 3.x, simply use the following line. 如果您使用的是Express 3.x,只需使用以下行。 BodyParser used to come bundled with versions prior to Express 4.0 BodyParser以前与Express 4.0之前的版本捆绑在一起

app.use(express.bodyParser());

If you are using Express 4.x, You would need to require the body-parser module explicitly 如果使用Express 4.x,则需要明确要求body-parser模块

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

and then use it in your app 然后在您的应用中使用

app.use(bodyParser());

Rest of your code seems fine. 您的其余代码似乎还不错。

You need to use body-parser . 您需要使用body-parser

Roughly, this is what you need: 大致上,这是您需要的:

var app = require('express')();
var http = require('http').Server(app);
...
var bodyParser = require('body-parser')

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

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

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