排队握手Node js cmd erros后无法排队握手

时间:2019-07-25 00:55:51

标签: node.js ejs

我正在使用ejs和mysql数据库开发用户登录和注册系统。当服务器在localhost中运行时,我想创建数据库和表,但是当我运行项目并输入用户名和密码进行注册时,节点js cmd中出现以下错误。

 Microsoft Windows [Version 10.0.17134.885]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\Users\Khundokar Nirjor\Desktop\node-authentication>node server.js
The magic happens on port 8000
GET /signup 304 32.447 ms - -
events.js:180
      throw er; // Unhandled 'error' event
      ^

Error: Cannot enqueue Handshake after already enqueuing a Handshake.
    at Protocol._validateEnqueue (C:\Users\Khundokar Nirjor\Desktop\node-authentication\node_modules\mysql\lib\protocol\Protocol.js:221:16)
    at Protocol._enqueue (C:\Users\Khundokar Nirjor\Desktop\node-authentication\node_modules\mysql\lib\protocol\Protocol.js:138:13)
    at Protocol.handshake (C:\Users\Khundokar Nirjor\Desktop\node-authentication\node_modules\mysql\lib\protocol\Protocol.js:51:23)
    at Connection.connect (C:\Users\Khundokar Nirjor\Desktop\node-authentication\node_modules\mysql\lib\Connection.js:119:18)
    at Strategy._verify (C:\Users\Khundokar Nirjor\Desktop\node-authentication\config\passport.js:54:24)
    at Strategy.authenticate (C:\Users\Khundokar Nirjor\Desktop\node-authentication\node_modules\passport-local\lib\strategy.js:88:12)
    at attempt (C:\Users\Khundokar Nirjor\Desktop\node-authentication\node_modules\passport\lib\middleware\authenticate.js:341:16)
    at authenticate (C:\Users\Khundokar Nirjor\Desktop\node-authentication\node_modules\passport\lib\middleware\authenticate.js:342:7)
    at Layer.handle [as handle_request] (C:\Users\Khundokar Nirjor\Desktop\node-authentication\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\Khundokar Nirjor\Desktop\node-authentication\node_modules\express\lib\router\route.js:137:13)
Emitted 'error' event at:
    at Connection._handleProtocolError (C:\Users\Khundokar Nirjor\Desktop\node-authentication\node_modules\mysql\lib\Connection.js:426:8)
    at Protocol.emit (events.js:203:13)
    at Protocol._delegateError (C:\Users\Khundokar Nirjor\Desktop\node-authentication\node_modules\mysql\lib\protocol\Protocol.js:398:10)
    at Handshake.<anonymous> (C:\Users\Khundokar Nirjor\Desktop\node-authentication\node_modules\mysql\lib\protocol\Protocol.js:232:10)
    at Handshake.emit (events.js:203:13)
    at Handshake.Sequence.end (C:\Users\Khundokar Nirjor\Desktop\node-authentication\node_modules\mysql\lib\protocol\sequences\Sequence.js:78:12)
    at C:\Users\Khundokar Nirjor\Desktop\node-authentication\node_modules\mysql\lib\protocol\Protocol.js:236:14
    at processTicksAndRejections (internal/process/task_queues.js:75:11) {
  code: 'PROTOCOL_ENQUEUE_HANDSHAKE_TWICE',
  fatal: false

}

这是我的代码db.js ..

// config/database.js
module.exports = {
    'connection': {
        'host': 'localhost',
        'user': 'root',
        'password': ''
    },
    'database': 'mydb1',
    'users_table': 'users'
};

这是护照.js的代码

// config/passport.js

// load all the things we need
var LocalStrategy   = require('passport-local').Strategy;

// load up the user model
var mysql = require('mysql');
var bcrypt = require('bcrypt-nodejs');
var dbconfig = require('./database');
var connection = mysql.createConnection(dbconfig.connection);
connection.connect();
connection.query('USE ' + dbconfig.database);
//connection.end();
// expose this function to our app using module.exports
module.exports = function(passport) {

    // =========================================================================
    // passport session setup ==================================================
    // =========================================================================
    // required for persistent login sessions
    // passport needs ability to serialize and unserialize users out of session

    // used to serialize the user for the session
    passport.serializeUser(function(user, done) {
        done(null, user.id);
    });

    // used to deserialize the user
    passport.deserializeUser(function(id, done) {
        connection.connect();
        connection.query("SELECT * FROM users WHERE id = ? ",[id], function(err, rows){
            done(err, rows[0]);
        });
      //  connection.end();
    });

    // =========================================================================
    // LOCAL SIGNUP ============================================================
    // =========================================================================
    // we are using named strategies since we have one for login and one for signup
    // by default, if there was no name, it would just be called 'local'

    passport.use(
        'local-signup',
        new LocalStrategy({
            // by default, local strategy uses username and password, we will override with email
            usernameField : 'username',
            passwordField : 'password',
            passReqToCallback : true // allows us to pass back the entire request to the callback
        },
        function(req, username, password, done) {
            // find a user whose email is the same as the forms email
            // we are checking to see if the user trying to login already exists
            connection.connect();
            connection.query("SELECT * FROM users WHERE username = ?",[username], function(err, rows) {
                if (err)
                    return done(err);
                if (rows.length) {
                    return done(null, false, req.flash('signupMessage', 'That username is already taken.'));
                } else {
                    // if there is no user with that username
                    // create the user
                    var newUserMysql = {
                        username: username,
                        password: bcrypt.hashSync(password, null, null)  // use the generateHash function in our user model
                    };

                    var insertQuery = "INSERT INTO users ( username, password ) values (?,?)";
                    connection.connect();
                    connection.query(insertQuery,[newUserMysql.username, newUserMysql.password],function(err, rows) {
                        newUserMysql.id = rows.insertId;

                        return done(null, newUserMysql);
                    });
                   // connection.end();
                }
            });
           // connection.end();
        })
    );

    // =========================================================================
    // LOCAL LOGIN =============================================================
    // =========================================================================
    // we are using named strategies since we have one for login and one for signup
    // by default, if there was no name, it would just be called 'local'

    passport.use(
        'local-login',
        new LocalStrategy({
            // by default, local strategy uses username and password, we will override with email
            usernameField : 'username',
            passwordField : 'password',
            passReqToCallback : true // allows us to pass back the entire request to the callback
        },
        function(req, username, password, done) { // callback with email and password from our form
            connection.connect();
            connection.query("SELECT * FROM users WHERE username = ?",[username], function(err, rows){
                if (err)
                    return done(err);
                if (!rows.length) {
                    return done(null, false, req.flash('loginMessage', 'No user found.')); // req.flash is the way to set flashdata using connect-flash
                }

                // if the user is found but the password is wrong
                if (!bcrypt.compareSync(password, rows[0].password))
                    return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); // create the loginMessage and save it to session as flashdata

                // all is well, return successful user
                return done(null, rows[0]);
            });
           // connection.end();
        })
    );
};

这是代码create_database.js

/**
 * Created by barrett on 8/28/14.
 */

var mysql = require('mysql');
var dbconfig = require('../config/database');

var connection = mysql.createConnection(dbconfig.connection);

connection.query('CREATE DATABASE ' + dbconfig.database);

connection.query('\
CREATE TABLE `' + dbconfig.database + '`.`' + dbconfig.users_table + '` ( \
    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, \
    `username` VARCHAR(20) NOT NULL, \
    `password` CHAR(60) NOT NULL, \
        PRIMARY KEY (`id`), \
    UNIQUE INDEX `id_UNIQUE` (`id` ASC), \
    UNIQUE INDEX `username_UNIQUE` (`username` ASC) \
)');

console.log('Success: Database Created!')

//connection.end();

0 个答案:

没有答案