user3946530
user3946530

Reputation:

How to get GET multiple variables in Express.js on Node.js?

I am trying to get Form data from html page to Node Js server .

My html file is index.html

 <body>

        <nav>
<ul>
<li>
  <a href="#" class="button add">Add Product</a>
  <div class="dialog" style="display:none">
  <div class="title">Add Product</div>
  <form action="" method="get">
    <input id = "name" name="name" type="text" placeholder="Product Name"/>
    <input name="code" type="text" placeholder="Product Code"/>
    <input name="category" type="text" placeholder=" Category"/>
    <input name="brand" type="text" placeholder="Brand"/>
<input type="submit" value="Ok"/>
  </form>
</div>
</li>
<li class="radio">
  <a href="#" class="button active"></a>

  <a href="#" class="button"></a>

  <a href="#" class="button"></a>
</li>
</ul>
</div>        
</nav>
<p></p>
    <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>

        <script src="js/index.js"></script>




  </body>

after submitting the form there is no action perform by the server just get the url with all details

and My server is server.js

var express = require("express"),
    app = express(),
    bodyParser = require('body-parser'),
    errorHandler = require('errorhandler'),
    methodOverride = require('method-override'),
    hostname = process.env.HOSTNAME || 'localhost',
    port = parseInt(process.env.PORT, 10) || 4004,
    publicDir = process.argv[2] || __dirname + '/public';
var exec = require('child_process').exec;
var fs = require('fs');

//Show homepage
app.get("/", function (req, res) {
  res.redirect("/index.html");
  console.log("shubh ");
});
app.get("/index/", function (req, res) {
  res.redirect("/index.html");
  console.log("shubham ");
});

app.get("/index/:name", function (req, res){
  console.log("shubham batra");
   var keyword = req.query.code;
   console.log(keyword);
   //res.send('You sent the name "' + req.body.name + '".');
  console.log(res.body);
});
app.use(errorHandler({
  dumpExceptions: true,
  showStack: true
}));
//Search page
app.use(methodOverride());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: true
}));
app.use(express.static(publicDir));
app.use(errorHandler({
  dumpExceptions: true,
  showStack: true
}));

console.log("Server showing %s listening at http://%s:%s", publicDir, hostname, port);
app.listen(port);

Upvotes: 2

Views: 2764

Answers (3)

vmkcom
vmkcom

Reputation: 1668

change the app.get("/index/:name", function (req, res){

to app.get("/index/:name?", function (req, res){

that your route will match /index/ and /index/anyname routes

and change form action to "/" or something like "/action"

Upvotes: 0

Michael Westcott
Michael Westcott

Reputation: 480

Your form action is '', therefor the URL created by the form will be /?name=xxx&code=yyy etc

From your routing, it seems like you're expecting the url to be in the form of /xxx?code=yyy.

Make your form action '/search' and add the following JS.

app.get("/search", function (req, res){
  console.log("shubham batra");
   var keyword = req.query.code;
   console.log(keyword);
   //res.send('You sent the name "' + req.query.name + '".');  
});

As your making a get request, request.body won't be set.

Upvotes: 1

Manwal
Manwal

Reputation: 23836

You can use as multiple as you want like:

var code = req.query.code;
var category = req.query.category;
var brand = req.query.brand;

req.query if its GET request and req.body if its POST.

Upvotes: 0

Related Questions