How node. js reads MySQL data

Source: Internet
Author: User

Install the MySQL module first.

node. js is installed by default, the module file is placed in the/usr/local/lib/node_modules directory, in order to manage cheaply, the module is unified installation here.

$ cd /usr/local/lib         $ npm install mysql         

Program Files Mysql.js

var Client = require(‘/usr/local/lib/node_modules/mysql‘).Client;var client = new Client();client.user = ‘root‘;client.password = ‘‘;console.log(‘Connecting to MySQL...‘);client.query(‘USE tiny_shop‘);     //如果MySQL中没有库表,赶紧建。http = require("http");var server = http.createServer(function(request, response) {    response.writeHeader(200, {"Content-Type": "text/html"});    client.query(‘SELECT * FROM tags‘, function selectCb(err, results, fields) {          if (err) {              throw err;          }          var data = ‘‘;        for (var i=0; i<results.length; i++) {                      var firstResult = results[i];            data += ‘id: ‘ + firstResult[‘id‘]+‘tag: ‘ + firstResult[‘tag‘];         }         response.write(data);         response.end();    

Run

$ node mysql.js

In the browser, input http://localhost:8080, you can display the data in the database.

How node. js reads MySQL data

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.