0

I am unable to set the values of HTML form inputs on to my Node JS variable. Below is my JS code where I am not able to set the values of variables "hostname" and "port" which will in turn concatenate to a new variable called url.

console.log(url) prints just mongodb://:. which suggests that my HTML form values are not being stored in my Node JS variables.

var express     = require('express');        // call express
var app         = express();                 // define our app using express
var bodyParser  = require('body-parser');
var router      = express.Router();  

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var port = process.env.PORT || 8080;        

var router = express.Router();              

const dbname    = "admin";

var hostname ='';
var port2 ='';

app.post('/', function(req, res){ 
    hostname = req.body.hostname;
    port2 = req.body.port2;
});

var url ="mongodb://"+hostname+":"+port2;

Below is my html which is handling the form inputs

    <form class="clr-form" action="/" method="POST">
        <input type="text" name="hostname" placeholder="Enter Host Name" class="clr-input"> <br/>
        <input type="text" name="port2" placeholder="Enter Port" class="clr-input"><br/>
        <input type="submit" value="Submit" />
    </form>
1
  • You should move the URL creation inside of the app.post. The .js file will only run once, so your URL will be created at server startup. If you move it into the app.post, it will be created on each post request. Commented Apr 3, 2020 at 12:08

2 Answers 2

1

You don't need to redeclare the port2 again

var port2 ='';

app.post('/', function(req, res){ 
    var hostname = req.body.hostname;
    port2 = req.body.port2;
});

var url ="mongodb://"+hostname+":"+port2;
Sign up to request clarification or add additional context in comments.

15 Comments

Hi about that - I was doing trial and error probably forgot to change the code from when I declared the variable inside the function to make it accessible to within the function. The problem is I am unable to get the inputs I entered on my html to my node console
call this api from your html
My node app is listening at localhost port 8080. What value should my form action have? "localhost:8080"? Also where should app.post('/') route to?
Also I think I can achieve the same by making use req.query supported by express. Any idea about this?
yes you can access them with req.query.property_name
|
0
var hostname ='';
var port2 ='';

app.post('/', function(req, res){ 
     hostname = req.body.hostname;
     port2 = req.body.port2;
});

You should not declare variables inside the function again if you want to assign body values to your variables which one you will be using outside of scope.

1 Comment

Hi about that - I was doing trial and error probably forgot to change the code from when I declared the variable inside the function to make it accessible to within the function. The problem is I am unable to get the inputs I entered on my html to my node console

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.