I am using Expressjs version 4.I am getting 'undefined' on req.param. Here is my example : 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/new
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">
I also tried with req.body
and req.params
, but the answer is still same.
req.params
Refers to the variables in your route path.
app.get("/posts/:id", ...
// => req.params.id
Post data can be referenced through req.body
app.post("/posts", ...
// => req.body.email
This assumes you are using the bodyParser
middleware.
And then there is req.query
, for those ?query=strings
.
You can use req.param()
for either of the 3 above. The look up order is params
, body
, query
.
param is a function, not an object. So you need to use req.param('email');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With