1

I have a requirement to Post input data from the HTML page into my localhost server.

so in my HTML, I'm using the "form"

like that:

<form action="/log-in",method="POST">
        <h3 style='font-family: Antic Slab; font-size: 23px;'>Log in to Your Account Now</h3>


        <div class="input-group mb-3">
          <div class="input-group-prepend">
            <span class="input-group-text" id="basic-addon1"><span class="material-icons">
                account_circle
              </span></span>

            <input name="Email1" style="width: 280px;" type="text" id="email" placeholder="Email or phone or username"
              title="Enter valid email with @, phone, or username">

          </div>
        </div>

        <div class="input-group mb-3">
          <div class="input-group-prepend">
            <span class="input-group-text" id="basic-addon1"><span class="material-icons">
                https
              </span></span>
            <input name="Password1" style="width:280px;" type="Password" id="password" placeholder="Password"
              pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}(?=.*[!@#$%^&*()-_=+\|[]{};:/?.><])"
              title="Must contain at least one number,Special charecter and one uppercase and lowercase letter, and at least 6 or more characters"><br>
          </div>
        </div>


        <div class="reset">
          <a id="Change"
            href="https://www.google.com/search?q=reset+password&oq=reset+password&aqs=chrome..69i57j69i59l2j69i60l2j69i61j69i60l2.2323j0j7&sourceid=chrome&ie=UTF-8">Forget
            Password?</a><br>
        </div>

        <div class="but">
          <input id="LGBTN" data-toggle="modal" data-target="#myModal" type="submit"
            class="btn btn-primary btn-lg btn-block" style='font-family: Antic Slab' value="Log-In"></button>
        </div>
      </form>

I have here 2 inputs (Email and password), when I'm using "GET" instead of post, I see the information passed throw the URL, but when I'm using POST and my NODEJS file look like that:

var http = require('http');
var url = require('url');
var fs= require('fs');
var express =require('express');
var app = express();

app.use(express.static(__dirname+ '/public'));

app.get('/log-in',function(req,res){
res.sendFile(__dirname+"/LogIn.html",);
});

)
app.post("/log-in",function(req,res){
  var email = req.body.Email1;
  res.redirect('/Contact-us');
 
})

app.listen(8080);

When I'm executing the code, I'm getting an Undefined error, and it says that he can't find the Variable "Email1". If I execute the code and just look at what I have in the Req, I see that the "param" array is empty like nothing has passed through.

is my approach to the data is wrong? because when using "GET" instead of "POST" I really see the email and the password in the URL, but when performing "POST" it's not working.

(I tried using Jquery to post some data too in this way:

$(document).ready(function () {
          $("#LGBTN").click(function () {
           $.post( "log-in", { name: "John", email: "test@test.com" } );
            });
              });

and still, there is nothing in the req. body, tell me its undefined, not getting the name and email like that )

any suggestions?

Ilan
  • 45
  • 3
  • Search on google for Body-parser – Prakhar Londhe Nov 28 '20 at 11:29
  • 1
    Does this answer your question? [How to process POST data in Node.js?](https://stackoverflow.com/questions/4295782/how-to-process-post-data-in-node-js) – Prakhar Londhe Nov 28 '20 at 11:31
  • 2
    Voting to close - duplicate of [Express.js req.body undefined](https://stackoverflow.com/questions/9177049/express-js-req-body-undefined). Please research thoroughly before posting new questions; this exact question has been addressed numerous times before. – esqew Nov 28 '20 at 13:49

4 Answers4

0

Using POST, variables goes to the body request. You need something to read it, express don't do it directly. But they have a middleware for that.

http://expressjs.com/en/resources/middleware/body-parser.html

farvilain
  • 2,552
  • 2
  • 13
  • 24
0
app.use(bodyParser.urlencoded({ extended : true }));
  • 2
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Nov 28 '20 at 13:33
0

app.use(bodyParser.urlencoded({ extended : true })); - This is an old way of doing it

app.use(express.json())

This make sure the body of the form is parsed into an accessible object chained with the request method.

0

The body of a POST request is received asynchronously as a stream. That means you need middleware to parse it before you can do any processing of its data. Options such as body-parser or express.json work well.

tgall
  • 132
  • 4