Получение и запись в переменную данных node.js

Рейтинг: -1Ответов: 1Опубликовано: 13.06.2023

при нажатии кнопки send нужно передать и записать данные, полученые с input полей в переменные (используется node.js)

    <h2 class="form-title">Input</h2>

    <div class="form-group">
      <input type="text" id="RecipAcNum" class="form-input" name="inpt" placeholder=" ">
      <label for="name" class="form-label">Recipient</label>
    </div>

    <div class="form-group">
      <input type="number" step="0.001" id="Amount" class="form-input" name="amount" placeholder=" ">
      <label for="name" class="form-label">Amount</label>
    </div>
  
      <input type="submit" class="form-button" value="send">

  </form>

Ответы

▲ -1Принят

Если я правильно понял вопрос, то вот ответ

const express = require('express');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.urlencoded({ extended: true } ));

app.get('/', (req, res) => {
   res.send(`
<h2 class="form-title">Input</h2>
<form action="/" method="post">
  <div class="form-group">
    <input type="text" id="RecipAcNum" class="form-input" name="recipient" placeholder=" ">
    <label for="name" class="form-label">Recipient</label>
  </div>
  <div class="form-group">
    <input type="number" step="0.001" id="Amount" class="form-input" name="amount" placeholder=" ">
    <label for="name" class="form-label">Amount</label>
  </div>
  <input type="submit" class="form-button" value="send">
</form>
  `);
});

app.post('/', (req, res) => {
const recipient = req.body.recipient;
const amount = req.body.amount;

 // Do something with the received data
console.log('Recipient:', recipient);
console.log('Amount:', amount);

// Send a response back to the client
 res.send('Data received successfully.');
 });

 const port = 3000;
 app.listen(port, () => {
    console.log(`Server is running on port ${port}`);
 });