我在req.body.numbers上不确定。我们如何在nodejs中获得textarea的值。
<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);
答案 0 :(得分:0)
<form>
<textarea>
或<input>
的值。
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>
答案 1 :(得分:0)
此post将帮助您了解如何将数据从HTML发送到node.js代码。
已编辑
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-供您参考
"body-parser": "^1.19.0",
"express": "^4.17.1"
上面的代码正在工作,并且接受req.body.numbers
中的textarea数字id。 urlencoded扩展值为true extended: true
而不是extended: false