0

First, Sorry if I got the title wrong, I am new and dunno how to start and the terms, what I want to do is

<form action="/" method="POST">
        <select name="cash" id="first">
        <option value="AED">AED</option>...

get the value of 'cash' i.e AED

app.post("/", function(.............) {
    request(..............)  
    var cash = req.body.cash;
                console.log(cash);
    
                var data = JSON.parse(body);
                var price = data.rates.cash;
    
                console.log(price);
                res.send( price);}

make a post request and make api call select the price(AED) and res.send and "How to retrieve POST query parameters?" this didn't help me.

    const express = require('express');
const app = express();
var bodyParser = require('body-parser');
const request = require('request');
app.use(express.json());
app.use(bodyParser.urlencoded({
    extended: true
}));
vvvvv
  • 25,404
  • 19
  • 49
  • 81
wakizu
  • 11
  • 2

2 Answers2

0

You can do something like this.

<form method="post" action="http://127.0.0.1:8080/">
    <select name="cash" id="first">
    <option value="AED">AED</option>
    <input type="submit" value="Submit">
</form>

Enter full URL like

<form action="http://127.0.0.1:8080/" method="post">

Node js

var express = require('express');
var bodyParser = require('body-parser');
var app     = express();

//Note that in version 4 of express, express.bodyParser() was
//deprecated in favor of a separate 'body-parser' module.
app.use(bodyParser.urlencoded({ extended: true })); 

//app.use(express.bodyParser());

// access your body data here
app.post('/', function(req, res) {
  res.send('You sent the code "' + req.body.cash + '".');
});

app.listen(8080, function() {
  console.log('Server running at http://127.0.0.1:8080/');
});
xMayank
  • 1,875
  • 2
  • 5
  • 19
0

As I understand correctly you want to:

  1. POST form data to backend
  2. during data processing, request 3rd party API for a price
  3. return a price to the user

The form view index.ejs:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
    <form action="/" method="POST">
        <select name="cash" id="first">
            <option value="AED">AED</option>
            <option value="USD">USD</option>
        </select>
        <button id="submit" type="submit">Submit</button>
    </form>

    <% if (cash && price) { %>
        <p>The price for <%= cash %> is <%= price %>.</p>
    <% } %>
</body>

</html>

I'm using EJS to conditionally display the price below the <form>.

Here is Express backend:

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
// I advise you to use axios instead of request for convenience
const axios = require('axios');

app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs');

app.get('/', function (req, res) {
    if (req.query.cash && req.query.price) {
        res.render('index', { cash: req.query.cash, price: req.query.price });
    }

    res.render('index', { cash: null, price: null });
});

app.post('/', async function (req, res) {
    const cash = req.body.cash;

    try {
        const response = await axios.post(
            'paste_your_url', { cash }
        );

        res.redirect(`/?cash=${cash}&price=${response.data.price}`);
    } catch (err) {
        console.log(err);
    }
});

app.listen(3000);

When the user enters the page /, Express renders index.ejs view without price information.

When the user submits the form to backend, Express:

  1. reads the data,
  2. requests 3rd party API,
  3. reads the price from 3rd party response,
  4. redirects user back to / with values in query string ?cash=<value>&price=<value>. This let Express render index.ejs with price information.

Also, I used axios instead of request, because it makes code more readable and easier (due to async/await).

macborowy
  • 1,474
  • 10
  • 13