I am getting undefined on req.body.numbers. How can we get the value of textarea in nodejs.
<form method="POST" action="/messaging">
<h4>Mobile Numbers</h4>
<textarea id="numbers" class="number-area" name="numbers"></textarea>
<h4>Add a Message</h4>
<textarea id="message" class="message-area" name="message"></textarea>
<br>
<div>
<button class="btn btn-outline-success" type="submit">
Send
</button>
</div>
</form>
exports.postMessaging = (req, res, next) => {
const numbers = req.body.numbers;
console.log(numbers);
<form>
<textarea>
or <input>
with express and body-parser . const express = require("express"); const app = express(); const bodyParser = require("body-parser"); const port = 3000; app.use(bodyParser.urlencoded({extended: false})); app.post("/link", (req,res) => { console.log(req.body.numbers); // body came with body-parser // numbers is name of textarea }); // req is request and res is response app.listen(port, () => { console.log("server launched"); });
<h4>Mobile Numbers</h4> <form action="/link" method="POST"> <textarea id="numbers" class="number-area" name="numbers"></textarea> <button type="submit">Submit</button> </form>
This post will help you to understand, how to send data from HTML to node.js code.
Edited
App.js
const express = require('express');
const bodyParser = require('body-parser');
const app = exports.module = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/test', function(req, res) {
var lName = req.body.lname;
var number = req.body.numbers;
res.send();
})
app.listen(1337, function() {
console.log('App is running on 1337');
})
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="http://localhost:1337/test" method="POST">
<input name="lname" id="lname"/>
<textarea id="numbers" class="number-area" name="numbers"></textarea>
<button type="submit">Submit</button>
</form>
</body>
</html>
package.json - for your reference
"body-parser": "^1.19.0",
"express": "^4.17.1"
The above code is working and accepting textarea numbers id in req.body.numbers
. The urlencoded extended value is true extended: true
not extended: false
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.