我正在使用 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.body
和 req.params
,但答案还是一样。
最佳答案
req.params
指的是你的路由路径中的变量。
app.get("/posts/:id", ...
// => req.params.id
可以通过req.body
app.post("/posts", ...
// => req.body.email
这假设您使用的是 bodyParser
中间件。
然后是 req.query
,对于那些 ?query=strings
。
您可以将 req.param()
用于上述 3 个中的任何一个。查找顺序为params
、body
、query
。
关于node.js - 获取 req.param 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23548929/