未定义Node.js,Express,HTML形式req.body [英] Node.js, express, html form req.body is undefined

查看:91
本文介绍了未定义Node.js,Express,HTML形式req.body的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么req.body未定义.它始终访问node.js服务器"/formstuff"中的函数,但req.body未定义.

I don't understand why req.body is undefined. It always accesses the function in the node.js server '/formstuff' but the req.body is undefined.

当我查看结果时,它确实发布到了cmd,但我在任何地方都看不到我的查询.实际上res.query和res.params是空的.

When I look at the results it did post to the cmd I don't see my query anywhere. in fact res.query and res.params are empty.

任何帮助将不胜感激,谢谢.

Any help would be appreciated, thanks.

快递:

var express = require('express'),
app = express();
var fs = require('fs'); 
var Promise = require('promise');



// Handle Get Request
app.get('/', function(req, res){
// get stuff from request
var index;

fs.readFile('./form.html', function (err, data) {
if (err) {
    throw err;
}
    index = data;

    res.setHeader("Content-Type", "text/html");
    res.send(index);

});

});


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

    res.send();
});

HTML:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 <style type = "text/css">

body
{
    margin:0;
}
#head
{   
    width:100%;
    color:#FAFAFA;
    height:170px;
    text-shadow: 2px 2px 0px rgba(150, 150, 150, 1);
    background: #39d800; /* Old browsers */
    background: -moz-linear-gradient(top,  #39d800 0%, #00a008 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#39d800), color-stop(100%,#00a008)); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top,  #39d800 0%,#00a008 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top,  #39d800 0%,#00a008 100%); /* Opera 11.10+ */
        background: -ms-linear-gradient(top,  #39d800 0%,#00a008 100%); /* IE10+ */
    background: linear-gradient(to bottom,  #39d800 0%,#00a008 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#39d800', endColorstr='#00a008',GradientType=0 ); /* IE6-9 */
    font-family: Brush Script MT;
    font-weight:bold;
    font-size:8.5em;
    text-align:center;
}

form div
{
    padding:0.8%;
}
fieldset
{
    font-size: 2em;
    position:relative;
}
input
{
    width: 150px;
    height: 50px;
    font-size: 1.2em;
    clear:both;
}
 </style>
</head>
<body>

<div id = "head">Notifye</div><br>

<br>

 <fieldset>
    <legend>Search</legend>

        <form name = "form" method = "post" action = "http://127.0.0.1:3000/formstuff">

        <input type="text" required = "required" name = "tag" placeholder = "lolcats">
        <div><input type = "submit" name = "submit" value = "Submit" id = "sub"></div>

    </form>

 </fieldset>

</body>

推荐答案

您缺少解析中间件的正文.

You're missing body parsing middleware.

如果您不上传文件,则只需npm install body-parser,然后在任何路由之前添加app.use(require('body-parser').urlencoded());.

If you're not uploading files, you can just npm install body-parser and then add app.use(require('body-parser').urlencoded()); before any of your routes.

这篇关于未定义Node.js,Express,HTML形式req.body的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆