javascript - 如何通过 Body-parser 获取 post 值 - Express Node.js

标签 javascript node.js express body-parser

我正在使用 表达 4.11.1,以及 正文解析器 1.11.0。 当我运行以下代码时,我得到以下输出

请建议如何获取表单值

输出

{}

server.js

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

var app = express();
var router = express.Router();

app.use(bodyParser.json(),bodyParser.urlencoded({ extended: true }));
// Routes

app.get('/', function(req, res){
    res.sendFile(__dirname + '/index1.html');
});

app.post('/', function(req, res){    
    console.log(req.body);
    res.send(req.body);
    res.sendFile(__dirname + '/index1.html');
});

app.listen(3000,function(){
    console.log("Working on port 3000");
});

index1.html

<!doctype html>
<html>
  <body>
<form id="frmTest" name="frmTest" action="http://localhost:3000/" method="post">
  <input type="text" id="mytext" value="sadfsad fsd fsad" />
  <input type="submit" id="mysubmit" />
</form>
  </body>
</html>

最佳答案

要在后端检索数据,您只需将名称添加到输入字段

<!doctype html>
<html>
  <body>
<form id="frmTest" name="frmTest" action="http://localhost:3000/" method="post">
  <input type="text" id="mytext" name="mytext" value="sadfsad fsd fsad" />
  <input type="submit" id="mysubmit" />
</form>
  </body>
</html>

关于javascript - 如何通过 Body-parser 获取 post 值 - Express Node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28250316/

相关文章:

javascript - 使用 JSONP 时的 CORB

javascript - 使用 javascript 检测适当的按钮

node.js - http ://localhost:8080/is not working

javascript - 使用 JavaScript 中的 API,它使用 JS 最大整数以上的 ID

node.js - JSON 中视频长度位置 0 处出现意外标记 G

javascript - 获取提交表单的操作

javascript - 在 nodeJs 中,有没有一种方法可以在不使用数组大小​​的情况下循环遍历数组?

node.js - session 存储在哪里? (ExpressJS)

javascript - 如何在 SailsJS 中有选择地包含 javascript Assets ?

node.js - Connect-Flash 和 Express 给了我多条消息但没有输入