vikash
vikash

Reputation: 55

How can I send data from client to server

I am new to nodejs.please help. Here is my html and node.js code. I want to send email and password from login.html(client) to login.js(server) and validate from table storing email and password.

login.html

<!DOCTYPE html>
<html>
<head>  
  <link rel="stylesheet" type="text/css" href="/loginstyle.css" />
</head>
<body>
  <form  class="css" method="post" action="http://localhost:9383/valid" >
    <fieldset >
      <input type="text" name="email" class="inputemail"  placeholder="Email" />
    </fieldset> 
    <fieldset >
      <input type="password" name="pass" class="inputpassword" placeholder="Password" />
    </fieldset >
    <fieldset >
      <button type="submit" class="submit">Submit</button>
    </fieldset>
  </form>
</body>
</html>

login.js

var express = require('express')
, app = express()
, mysql = require("mysql");

app.get('/loginme', function (req, res) {
  res.sendfile(__dirname + '/loginmysql.html');
});

app.post('/valid', function (req, res) {
  console.log("hello");
  var username=req.body.email;
  var password=req.body.pass;

    var connection = mysql.createConnection({
      "hostname": "localhost",
      "user": "root",
      "password": "123",
      "database": "login"
    });

    connection.connect();

    connection.query('SELECT * FROM id WHERE email=? AND password=?', [username,password], function(err, rows){

      if (err){
        throw err;
      }else{
        for (var i in rows) {
           console.log('name: ', rows[i].name);
           name=rows[i].name;
           res.redirect('/option');

        }
      }

    });

     connection.end();
});

Upvotes: 0

Views: 116

Answers (1)

mscdex
mscdex

Reputation: 106746

You're missing a form parsing middleware. Try adding the body-parser middleware somewhere before your POST route.

Upvotes: 4

Related Questions