node.js - 获取 req.param 未定义

标签 node.js express

我正在使用 Expressjs 版本 4。我在 req.param 上得到“未定义”。这是我的例子: app.js

var express = require('express');
var bodyParser = require('body-parser');
var newdata = require('./routes/new');
........................
......................
app.use(bodyParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());

app.use('/new', newdata);

./routes/新

var express = require('express');
var router = express.Router();

router.get('/', function(req, res){
    res.render('newdata', {
        title: 'Add new data'
    })
});

router.post('/', function(req, res){
    console.log(req.param['email']);
    res.end();
});

module.exports = router;

newdata.html

<form action="/new" role="form" method="POST">
            <div class="form-group">
                <label for="exampleInputEmail1">Email address</label>
                <input type="email" class="form-control" name="email" placeholder="Enter email">

我也试过 req.bodyreq.params ,但答案还是一样。

最佳答案

req.params 指的是你的路由路径中的变量。

app.get("/posts/:id", ...

// => req.params.id

可以通过req.body

引用post数据
app.post("/posts", ...

// => req.body.email

这假设您使用的是 bodyParser 中间件。

然后是 req.query,对于那些 ?query=strings


您可以将 req.param() 用于上述 3 个中的任何一个。查找顺序为paramsbodyquery

关于node.js - 获取 req.param 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23548929/

相关文章:

javascript - Express类型错误: Cannot read property 'get' of undefined

node.js - 需要理解为什么expressjs重定向到index.html

node.js - 一旦我开始将文件添加到 Controller 文件夹中,代码就会中断 - 为什么?

javascript - $apply 使用时范围未更新

node.js - Node 服务器路由全部超时

javascript - Node.js hubot 在服务器上执行命令

node.js - Nodejs 分段文件上传,生成的文件比原始文件大

node.js - Jade 模板中静态文件的路由路径的 Express/Node 问题

javascript - 在我的案例中如何连接到 elasticache?

node.js - 我应该在我的 REST Api 中使用 Helmet 的哪些模块