node.js+mysql+express实现登入注册页面前后端交互

前端部分

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>登入注册</title>

    <style>

        *{

            margin: 0;

            padding: 0;

        }

        body{

            height: 550px;

            background-image: url('../../img//002R448Kgy1hkeh2jokj4j62dc4qnnpg02.jpg');

            background-size: 100%;

            background-repeat: no-repeat;

            background-position: center;

            position: relative;

        }

        .box{

            width: 400px;

            height: 500px;

            border: 2px solid black;

            position: absolute;

            left: 50%;

            top: 50%;

            margin-left: -200px;

            margin-top: -250px;

        }

        .box form{

            width: 100%;

            height: 100%;

        }

        .box form h1{

            text-align: center;

            color: aliceblue;

        }

        .box form span{

            display: inline-block;

            width: 70px;

            height: 20px;

            /* border: 1px solid red; */

            margin-left: 40px;

            margin-top: 40px;

            text-align: right;

        }

        .box form input{

            height: 20px;

            outline: none;

        }

        .box .tress{

            width: 50px;

            margin-left: 130px;

            margin-top: 40px;

            text-align: center;

            font-weight: 600px;

            cursor: pointer;

        }

        .box .four a{

            text-decoration: none;

            border: 0.1px solid black;

            color:red;

            font-size: 14px;

            font-weight: 600px;

            text-align: center;

            cursor: pointer;

            background-color: aliceblue;

        }

    </style>

</head>

<body>

        <div class="box">

            <form action="http://localhost:8080/login" method="get">

                <h1>登入</h1>

                <span>用户名:</span><input type="text" name ='account'><br>

                <span>密码:</span><input type="password" name="password"><br>

                <!-- <span>确认密码:</span><input type="password" ><br> -->

                <input type="submit" value="登入" class="tress">

                <span class="four"><a href="../public/resing.html">点击注册</a></span>

            </form>

        </div>

</body>

</html>

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>注册</title>

    <style>

        *{

            margin: 0;

            padding: 0;

        }

        body{

            height: 550px;

            background-image: url('../../img//001YqBPrly1hk56d2pcdej644o5pukju02.jpg');

            background-size: 100%;

            background-repeat: no-repeat;

            background-position: center;

            position: relative;

        }

        .box{

            width: 400px;

            height: 500px;

            border: 2px solid black;

            position: absolute;

            left: 50%;

            top: 50%;

            margin-left: -200px;

            margin-top: -250px;

        }

        .box form{

            width: 100%;

            height: 100%;

        }

        .box form h1{

            text-align: center;

            color: aliceblue;

        }

        .box form span{

            display: inline-block;

            width: 100px;

            height: 20px;

            /* border: 1px solid red; */

            margin-left: 40px;

            margin-top: 40px;

            text-align: right;

        }

        .box form input{

            height: 20px;

            outline: none;

        }

        .box .tress{

            width: 50px;

            margin-left: 130px;

            margin-top: 40px;

            text-align: center;

            font-weight: 600px;

            cursor: pointer;

        }

        .box .four{

        }

        .box .four a{

            text-decoration: none;

            border: 0.1px solid black;

            color:red;

            font-size: 14px;

            font-weight: 600px;

            text-align: center;

            cursor: pointer;

            background-color: aliceblue;

        }

    </style>

</head>

<body>

    <div class="box">

        <form action="http://localhost:8080/process" method="get">

            <h1>注册</h1>

            <span>用户名:</span><input type="text" name ='account'><br>

            <span>密码:</span><input type="password" name="password"><br>

            <span>确认密码:</span><input type="password" name="repassword"><br>

            <input type="submit" value="注册" class="tress">

            <span class="four"><a href="../public//index.html">返回登入</a></span>

        </form>

    </div>

</body>

</html>

后端部分

服务器启动部分

const express = require('express')

const router = require('./router')

const cors = require('cors')

const server = express()

erver.use(cors())

server.use('/',router)

server.listen(8080,()=>{

    console.log('server start');

})

动态路由部分

const express = require('express')

const {index,register} = require('./index')

const router = express.Router()

router.get('/login',index)

router.get('/process',register)

module.exports = router

数据库连接部分

const mysql = require('mysql')

const db = mysql.createConnection({

    host:'localhost',

    port:3306,

    user:'root',

    password:'197386',

    database: 'node'

})

module.exports = db

动态路由请求部分

const mysql = require('./mysql')

const index =function (req,res) {

    var response = {

       "account":req.query.account,

       "password":req.query.password,

    };

     var selectSQL = "select name,password from user where name = '"+req.query.account+"' and password = '"+req.query.password+"'";

    mysql.query(selectSQL,function (err, result) 

        if(err){

         console.log('[login ERROR] - ',err.message);

         return;

        }
        //如果查
询结果为空,则登录失败,否则登录成功

        if(result=='')

        {

            console.log("帐号密码错误");

            res.end("fail");

        }

        else

        {

            console.log("登录成功");

            res.end("success");

        }

    });

    console.log(response);

}

const register = function(req,res,next){

    var result = {

        "account":req.queryaccount,

        "password":req.querypassword,

        "repassword":req.query.repassword

    }

    if(req.query.password === req.query.repassword){

    var selectSQL = 'INSERT INTO user(name,password,repassword) VALUE (?,?,?)';

    var addSqlParams = [req.query.account,req.query.password,req.query.repassword]

    mysql.query(selectSQL,addSqlParams,function (err, result) {

        // 打印错误信息

        if(err){

         console.log('[INSERT ERROR] - ',err.message);

         res.end("fail");

         //如果失败就直接return不会执行下面的代码

         return;

        }

        res.end("success");

        console.log("注册成功");


 

    });

    console.log(result);

    }else{

        console.log("帐号密码错误");

        res.end('password not repassword')

    }

}



 

module.exports = {

    index,

    register

}

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
前后端分离是一种开发模式,它将前端和后端的开发分离,前端主要负责用户界面的展示和交互,后端主要负责数据的处理和存储。Vue2、Node.jsMySQL可以结合使用来实现前后端分离。 首先,我们可以使用Vue2作为前端框架,通过它来开发用户界面。Vue2提供了一套响应式的数据绑定和组件化的架构,使得前端开发更加高效和灵活。我们可以使用Vue的官方脚手架工具vue-cli来快速搭建项目的基础结构。 其次,Node.js可以用作后端技术,作为一个基于事件驱动的服务器端JavaScript运行环境,它提供了丰富的模块和工具,使得后端开发更加便捷。我们可以使用Express框架来构建Node.js的后端应用,通过定义路由和处理请求,与前端进行数据的交互。 最后,MySQL是一个开源的关系型数据库管理系统,它可以存储和管理数据。我们可以使用Node.jsmysql库来连接和操作MySQL数据库,通过编写SQL语句来实现数据的增删改查。 在实际开发中,前端通过Ajax或者Axios等工具向后端发送请求,后端接收请求后,通过与MySQL数据库的交互来获取或处理数据,并将结果返回给前端。前端通过Vue2的数据绑定和渲染机制,将后端返回的数据展示在用户界面上。 通过Vue2、Node.jsMySQL的组合,我们可以实现一个完整的前后端分离的应用程序。Vue2提供了优秀的用户界面,Node.js作为后端技术提供了强大的功能和灵活性,MySQL作为数据库管理系统提供了数据的存储和管理。这样的开发模式可以提高开发效率和代码的维护性,同时也能够实现更好的用户体验和性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值