[NodeJS]---cqManger background management system

Project requirements

Hero backstage management

Implement functions address
Login /login.html
homepage /index.html
New /insert.html
register /register.html
update /update.html

1, Project presentation

  1. register
  2. Login
  3. homepage
  4. update
  5. New

    2, Interface information
function address explain
Determine whether to log in /isLogin Judging login interface based on third-party module cookie session
Query all information /index.html Query based on mysql module
edit //hero/update Update based on mysql module
delete //hero/delete Soft deletion of isDelete key in mysql
Verification code /captcha Verification code generation based on the third-party module: SVG captcha
User registration /hero/register Password encryption, registration and writing to the database
Login /user/login Query database information and verify
Log out /logout Log out

3, Interface details

  • Login interface:
    It is mainly used to verify whether you have logged in through data query in the database, and set the logged in coat information
app.post('/user/login', (req, res) => {
    let { username, password } = req.body
    userModel.find(`username="${username}" and password="${password}"`, (err, results) => {
        if (err == null) {
            if (results.length > 0) {
                // Set cooike
                req.session.user = { username, password };
                res.send({
                    code: 200,
                    msg: 'Login successful'
                })
            } else {
                res.send({
                    code: 401,
                    msg: 'Wrong account or password'
                })
            }
        } else {
            res.send({
                code: 500,
                msg: 'Server error'
            })
        }
    })

})
  • Log out
    It mainly clears the cooike information and redirects to the login page
app.get('/logout', (req, res) => {
    // Clear cooike
    req.session = null
    // redirect
    res.writeHead(302, {
        'Location': './login.html'
    })
    res.end()
})
  • delete
    It mainly performs a soft deletion, that is, modifying the isDelete item of the data
app.post('/hero/delete', (req, res) => {
    let { id } = req.body
    heroModel.update(`id=${id}`, { isDelete: 'true' }, (err, results) => {
        if (err == null) {
            res.send({
                code: 200,
                msg: 'Deletion succeeded'
            })
        } else {
            res.send({
                code: 500,
                msg: 'Server error'
            })
        }
    })
})
  • Query all information
    This is mainly used to query items that have not been soft deleted, and it also supports more keyword queries, namely (fuzzy queries)
app.get('/hero/list', (req, res) => {
    //Query the current cookie
    let { search } = req.query

    if (!search) {
        // undefined
        heroModel.find('isDelete="false"', (err, results) => {
            if (err == null) {
                res.send({
                    code: 200,
                    heros: results,

                })
            } else {
                res.send({
                    code: 500,
                    msg: 'Server error'
                })
            }
        });
    } else {
        heroModel.find(`heroName like '%${search}%' and isDelete ="false"`, (err, results) => {
            if (err == null) {
                res.send({
                    code: 200,
                    heros: results,

                })
            } else {
                res.send({
                    code: 500,
                    msg: 'Server error'
                })
            }
        })
    }

})
  • Determine whether to log in
    back-end
app.get('/isLogin', (req, res) => {
//Return the cooike information
    res.send(req.session.user)
})

front end
Judge whether it is empty to judge the login status

<!-- send ajax Request to determine whether to log in -->
  <script>
    $.ajax({
      type: 'get',
      url: 'http://127.0.0.1:4399/isLogin',
      success: function (backData) {
        if (backData == '') {
          // not logged on
          alert('You are not logged in please log in')
          window.location.href = './login.html'
        }
      }
    })
  </script>

Source code download

Tags: Javascript node.js JQuery

Posted by Dude0 on Mon, 30 May 2022 07:01:52 +0530