简体   繁体   中英

Javascript MySQL Connection on HTML Page

I'm using node.js's node-mysql driver for MySQL. I wrote this scrap of code

    var mysql = require("mysql");
    var connection = mysql.createConnection({
        host     : "localhost",
        user     : "root",
        password : "",
        database : "story"
    });
    function testQuery() {
        connection.query("INSERT INTO stories (TITLE, AUTHOR, STORY) VALUES        
        (\"hello\",\"goodbye\",\"sayonara\")", function(err,rows){
            if(err) throw err;
        }   )
        return;
    }

So when I run the code in the testQuery() function in the node.js command line and it works as expected inserting hello goodbye and sayonara into the mysql database. But when I place the script into a HTML page and have a button onclick run testQuery() I don't get any result.

You are trying to run Nodejs server code on the client, this will not work. Also this would mean you will become hacked instantly because your password is stored in encrypted, plain text and stored on the clients machine. Double no no. You must start a new node instance and serve this code from the server, not from the browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM