'Chatbot using javascript and mysql database

How do I build a chatbot using javascript that uses the MySQL database? I have created a simple chatbot but I don't know how to connect it with the database. I tried to connect the database using node.js but I don't know how to use that database connection in my javascript program. Please give me some ideas.T_T

var trigger=[
    ["hi", "hey", "hello"],
    ["how are you", "how are things"],
    ["fine"],
    ["what is going on", "what is up"],
    ["happy", "good", "well", "fantastic", "cool"],
    ["bad", "bored", "tired", "sad"],
    ["tell me story", "tell me joke"],
    ["thanks", "thank you"],
    ["bye", "good bye", "goodbye"],
    ["regno","registerno","register","reg"]
    ];
    var reply=[
    ["Hello!", "Hi!", "Hey!", "Hi there!"], 
    ["Fine... how are you?",
    "Pretty well, how are you?",
    "Fantastic, how are you?"],
    ["Good to hear that ^_^","Awesome"],
    ["Nothing much",
    "Exciting things!"],
    ["Glad to hear it"],
    ["Why?", "Cheer up buddy"],
    ["What about?", "Once upon a time..."],
    ["You're welcome", "No problem"],
    ["Goodbye", "See you later"],
    ["Looking up in the database"]
    ];
    var alternative = [
    "I am not programmed to those commands...Sorry T_T"
    ];
    document.querySelector("#input").addEventListener("keypress",function(e){
        var key = e.which || e.keycode;
        if(key === 13){
            var input = document.getElementById("input").value;
            document.getElementById("user").innerHTML = input;
            output(input);
        }
    });  

    function output(input){
        try{
            var product = input + "=" + eval(input);
        }
        catch(e){
            var text = (input.toLowerCase()).replace(/[^\w\s\d]/gi, "");
            text = text
                .replace(/ a /g, " ")
                .replace(/i feel /g, "")
                .replace(/whats/g, "what is")
                .replace(/please /g, "")
                .replace(/ please/g, "")
                .replace(/reg /g,"Registerno");
            if(compare(trigger,reply,text)){
                var product = compare(trigger,reply,text);
            }
            else{
                var product=alternative[Math.floor(Math.random()*alternative.length)];
            }
        }
        document.getElementById("chatbot").innerHTML=product;
        document.getElementById("input").value="";//clear the input value
    }
    function compare(arr, array, string){
        var item;
        for(var x=0; x<arr.length; x++){
            for(var y=0; y<array.length; y++){
                if(arr[x][y] == string){
                    items = array[x];
                    item = items[Math.floor(Math.random()*items.length)];
                }
            }
        }
        return item;
    }
body { 
    color: #421; 
    font-weight: bold; 
    font-size: 18px; 
    font-family: "Courier New"; 
    background: rgb(200, 232, 241); 


}
body::after {
    content: "";
    background-image: url("bot.png");
    background-repeat: repeat-y; 
    opacity: 0.5;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    position: absolute;
    z-index: -1;   
  }
span { 
    color: rgb(36, 17, 119); 
} 
::-webkit-input-placeholder { 
    color: #711 
}
#main { 
    position: fixed; 
    top: 40%; 
    right: 200px; 
    width: 400px; 
    border: 0px solid #421; 
    padding: 40px; 
}
#main div { 
    margin: 10px; 
} 
#input { 
    border: 0; 
    padding: 5px; 
    border: 1px solid #421; 
}
<!DOCTYPE html>
<html>
<head>
<title>Chatbot</title>
<link rel="stylesheet" href="style.css" />
<link href="database.js">
</head>
<body>
<div id="main">
    <div>user:<span id="user"></span></div>
    <div>bot:<span id="chatbot"></span></div>
    <div><input id="input" type="text" placeholder="Say something..." autocomplete="off"/></div>
</div>
<script type="text/javascript" src="index.js">    
</script>
</body>
</html>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source