1
votes

New Question!

I got the web page to display and was able to retrieve and display the user input.
However, my code only allows one user input, the rest will be undefined if I add another input field in the HTML.
Here is the updated code, which will only display the last name.

/*server.js*/
var express = require('express');
var bodyParser = require('body-parser');
var formHTML = require('./formHTML');

var app = express();
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies

app.get('/', function(req, res){
  var html = formHTML.html1;

  res.send(html);
});

app.post('/', function(req, res){
  var firstName = req.body.firstName;
  var lastName = req.body.lastName;
  var html = formHTML.html2;

  html = html.replace('firstName', firstName);
  html = html.replace('lastName', lastName);
  res.send(html);
});

app.listen(8080);


/*formHTML.js*/
var html1 = '<form action="/" method="post">' +
  'Enter your first name:' +
  '<input type="text" name="firstrName" placeholder="..." />' +
  '<br>' +
  'Enter your last name:' +
  '<input type="text" name="lastName" placeholder="..." />' +
  '<br>' +
  '<button type="submit">Submit</button>' +
  '</form>';

var html2 = 'Hello: firstName lastName .<br>' + '<a href="/">Try again.</a>';

exports.html1 = html1;
exports.html2 = html2;

What can I try to retrieve both user input?? Thanks!

=========================================================================

I have the following code that supposedly open a web page with one user input field, and use express to get and post display the user's input.

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

app.use(bodyParser());

app.get('/', function(req, res){
  var html = '<form action="/" method="post">' +
    'Enter your name:' +
    '<input type="text" name="userName" placeholder="..." />' +
    '<br>' +
    '<button type="submit">Submit</button>' +
    '</form>';

  res.send(html);
});

app.post('/', function(req, res){
  var userName = req.body.userName;
  var html = 'Hello: ' + userName + '.<br>' +
    '<a href="/">Try again.</a>';
  res.send(html);
});

app.listen(80);

However, I got the following error and I cannot figure out how to get it to work. Can someone give me a hint? Thanks!

Thu, 02 Jun 2016 15:10:23 GMT body-parser deprecated bodyParser: use individual json/urlencoded middlewares at server.js:20:9 Thu, 02 Jun 2016 15:10:23 GMT body-parser deprecated undefined extended: provide extended option at ....\node_modules\body-parser\index.js:105:29

2

2 Answers

4
votes

The bodyParser() constructor has been deprecated (details here), you need to use these now :

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

When extended is set to true, it will use the qs library for parsing, and when set to false it will use querystring. Research the difference between the two to decide which one you want to use, I've always used qs without any issues.

0
votes

change the :

app.use(bodyParser());

with :

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