首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >我写了一个类似于教程中解释的代码,但它不起作用。描述中包含的错误

我写了一个类似于教程中解释的代码,但它不起作用。描述中包含的错误
EN

Stack Overflow用户
提问于 2020-09-14 12:09:25
回答 2查看 42关注 0票数 0
代码语言:javascript
代码运行次数:0
运行
复制
    let express=require("express") 
    let ourApp=express()
    ourApp.get('/',function(req,res) {
        res.send(
            `<form action="/answer" method="POST">
            <h1>What is the name of tallest mountain in the world?</h1>
            
        <input name="correct">
        <button>Click here to submiit</button>   </form>`
        ) }) 
    ourApp.post('/answer',function(req,res){
        if(req.body.correct=="everest")
                res.send("Thats a correct answer")
             } ) 
    ourApp.listen(3000)

错误:

TypeError: Cannot read property 'correct' of undefined

EN

回答 2

Stack Overflow用户

发布于 2020-09-14 12:46:16

没错,您的应用程序需要一个解析器来解析请求正文。你需要一个中间件程序来解析你的请求体。在安装body-parser之后尝试。

安装后,您可以读取req.body,否则express将无法识别您的req.body对象,

代码语言:javascript
代码运行次数:0
运行
复制
let express = require("express");
let ourApp = express();
var bodyParser = require("body-parser");
ourApp.use(
  bodyParser.urlencoded({
    extended: true,
  })
);
ourApp.use(bodyParser.json());
ourApp.get("/", function (req, res) {
  res.send(
    `
        <form action="/answer" method="POST">
        <h1>What is the name of tallest mountain in the world?</h1>
    <input name="correct">
    <button>Click here to submiit</button>   </form>
    `
  );
});
ourApp.post("/answer", function (req, res) {
  if (req.body.correct.toLowerCase() == "everest") return res.send("<h2> Thats a correct answer </h2>");
  return res.send("<h2> Nope your are wrong </h2>");
});
ourApp.listen(3000);

试试这段代码。ourApp.use() -在你的应用程序中包含正文解析器中间件,请尝试阅读正文解析器here对你的应用程序做了一些小修改:)

票数 0
EN

Stack Overflow用户

发布于 2020-09-14 12:48:29

原因:- its not working because you need to add some middleware to parse the post data of the body.

代码语言:javascript
代码运行次数:0
运行
复制
ourApp.use(express.json());
    ourApp.use(express.urlencoded());

说明如何分析应用程序如何分析POST数据

我的一条建议。

  • 的早期版本曾经捆绑了许多中间件。bodyParser就是其中的一个中间件。当Express4.0发布时,他们决定从Express中删除捆绑的中间件,并将它们单独打包。

  • 安装bodyParser模块后,语法从app.use(express.json())更改为app.use(bodyParser.json())。

  • bodyParser在4.16.0版中被重新添加到Express中,因为人们希望像以前一样将它与Express捆绑在一起。这意味着如果您使用的是最新版本,则不必再使用bodyParser.json()。您可以改用express.json()。

试试这个,这个很管用。

代码语言:javascript
代码运行次数:0
运行
复制
let express=require("express");
let ourApp = express();
ourApp.use(express.json());
ourApp.use(express.urlencoded());
ourApp.get("/", function (req, res) {
  res.send(
    `<form action="/answer" method="POST">
        <h1>What is the name of tallest mountain in the world?</h1>
        
    <input type="text" name="correct" id='jatin'> 
    <button>Click here to submiit</button>  </form> `
  );
});

ourApp.post("/answer", function (req, res) {
  console.log(req.body);
  if (req.body.correct == "everest") res.send("Thats a correct answer");
});

ourApp.listen(3000, function (req, res) {
  console.log("server started");
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63877976

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档