我不知道为什么console.log( req.body )没有显示任何输出如果有人可以解释为什么当我填写详细信息并提交它时,req.body应该在终端中打印详细信息,但它没有显示任何内容我是nodejs的新手,请解释一下为什么它实际上没有打印输出
代码:
const express = require('express');
const path = require('path');
const app = express();
//express related stuff
app.use('/static',express.static('static'));
app.use(express.urlencoded());
//pug specific stuff
app.set('view engine', 'pug');
app.set('views',path.join(__dirname,'views'));
//endpoint
app.get('/',(req,res)=>{
const con = "knowledge";
const parans = {'title':'PUBG','content': con};
res.status(200).render('index.pug',parans);
});
app.post('/',(req,res)=>{
console.log(req.body);
const parans = {'message': 'your form has been submitted'};
res.status(200).render('index.pug',parans);
})
//listen,port
app.listen(80,()=>{
console.log("sucess on port 80");
});<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>#{title}</title>
style
include ../static/style.css
</head>
<body>
<nav>
<ul>
<li><a href="/">HOME</a></li>
<li><a href="/about">ABOUT</a></li>
<li><a href="/joinus">JOIN_US</a></li>
</ul>
</nav>
<h1>HEADING</h1>
<!--<h2>#{content}</h2>
<p>this is plain HTML using PUG</p>-->
<div class="container">
<form action="/" id="contact">
<input type="text" id="name" placeholder="ENTER YOUR NAME">
<input type="text" id="AGE" placeholder="ENTER YOUR AGE">
<input type="text" id="GENDER" placeholder="ENTER YOUR GENDER">
<button class="btn">Submit</button>
</form>
</div>
</body>
</html>
发布于 2021-07-08 16:10:13
express.urlencoded()不能解析那些没有name属性的输入域。
要解决这个问题,只需将HTML name属性添加到<form>内的每个输入标记即可。
示例:
<input name='name' type='text'>
<input name='age' type='number'>
^^^^^^^^^^
发布于 2020-11-17 15:48:07
首先安装npm,安装body-parser,然后添加以下行
Code:
const bodyParser=require("body-parser");
app.use(bodyParser.json());
app.use(express.urlencoded({ extnded: true }));https://stackoverflow.com/questions/64871076
复制相似问题