Nina watcher Nina watcher - 3 years ago 162
Node.js Question

Express, How to get value from html

I've node with express and I've simple index.html file which serves like following

app.js

app.use(express.static(path.join(__dirname, 'public')));

app.get('/', function(req, res){
res.sendFile(path.join(__dirname, 'views/index.html'));
});


app.post('/up', function(req, res){


....

index.html

<div class="container">
<div class="row">
<div class="col-xs-12">
<div class="panel panel-default">
<div class="panel-body">
<span class="glyphicon glyphicon-cloud-upload"></span>
File Name: <input type="text" name="FileName"><br>
<div class="progress">
<div class="progress-bar" role="progressbar"></div>
</div>
<button class="btn btn-lg up-btn" type="button">Run App</button>
</div>
</div>
</div>
</div>
</div>


when the user run the app there is input fields
file Name
which he can type value there , I want to get the value when the user click on the button
Run App

inside the post method in the
app.js
, How can I get it ?

I try with events without sucess

Answer Source

First of all you should have a form to your html with a button type submit:

<div class="container">
    <div class="row">
      <div class="col-xs-12">
        <div class="panel panel-default">
          <div class="panel-body">
            <span class="glyphicon glyphicon-cloud-upload"></span>
            <form action="up" method="POST">
              File Name: <input type="text" name="FileName"><br>
              <div class="progress">
                <div class="progress-bar" role="progressbar"></div>
              </div>
              <button class="btn btn-lg up-btn" type="submit">Run App</button>
            </form>
          </div>
        </div>
      </div>
    </div>
  </div>

Then on your server you need to have the body-parser package: https://www.npmjs.com/package/body-parser

Then into server side application you need this code:

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

app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser());

app.get('/', function(req, res){
    res.sendFile(path.join(__dirname, 'views/index.html'));
});


app.post('/up', function(req, res){
  var txt_folder_name = req.body.FileName;
  //Do other stuff
})
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download