Node.js+mysql displaying data to a simple instance of the front end

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.