req.body.username being undefined



在Express JS中,我正在使用正文解析器来获取用户名。 当我尝试获取用户名时,它返回未定义。谁能帮我?

app.use(session({
 cookieName: 'session',
 secret: "Shh_It's_a_secret",
 duration: 100000 * 100000
}));
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/home', (req, res) => {
if(req.body.username){
 res.send('Invalid Username, Press The Back Button To Try Again');
} else if (!req.body.username) {
  app.use(express.static(__dirname + '/NerdFestProjectHomepage.html'));
  res.sendfile('NerdFestProjectHomepage.html');
  console.log(req.body.username);
}
})

当我尝试获取 req.body.username 时,它返回未定义。

这是一个

GET请求。在表达中,无法使用GET请求发送"正文"。

更改 GET 到 POST 请求 :

app.post('/home', cb );

并着眼于使用:

<form action="/home" method="POST"></form>

最新更新