Originally thought the data came out, to show is a trivial thing! Who knows, I made a day .... I was wrong in not putting connection.query inside.
The following example is possible! Read my previous article of the classmate, should be very familiar with the following code, right! Mainly the red ones. Every time progress a little bit, a lot of time so a little bit, may be a great inspiration for the future is not necessarily.
Still used back to my old practice library:
----Database: ' Test '----------------------------------------------------------------Table structure ' mytable '--CREATE table ' Mytab Le ' ( ' name ' varchar (255) not NULL) Engine=myisam DEFAULT Charset=utf8;----export the data in table ' MyTable '--INSERT into ' mytable ' VALUES (' name 1 '); INSERT into ' mytable ' values (' Name 2 ');
Settings.js
var settings={};settings.db={ ' localhost ', ' root ', ' xxx ', database:' test '}module.exports=settings;
App.js
varExpress = require (' Express ');varApp =Express ();//connecting to a databasevarMySQL = require (' MySQL ');//Configuration ModulevarSettings = require ('./settings '));//connecting to a databasevarConnection =mysql.createconnection (settings.db); Connection.connect ();//EnquiryvarSelectsql = ' select * from ' mytable ';vararr =[];connection.query (Selectsql,function(err, rows) {if(ERR)Throwerr; for(vari = 0; i < rows.length; i++) {Arr[i]=Rows[i].name; } //output the search value app.get ('/', function (req, res) {res.send (arr); });});//Close Connectionconnection.end (); App.listen (3000);
After node, browse: Http://localhost:3000/
Node.js+mysql displaying data to a simple instance of the front end