如何从客户端向服务器发送数据

时间:2014-08-07 12:55:43

标签: javascript html node.js

我是nodejs.please帮助的新手。 这是我的html和node.js代码。我想从login.html(客户端)向login.js(服务器)发送电子邮件和密码,并从存储电子邮件和密码的表中进行验证。

的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();
});

1 个答案:

答案 0 :(得分:4)

您错过了解析中间件的表单。尝试在POST路由之前的某处添加body-parser中间件。