Node Connection database (express + mysql) _ node. js

Source: Internet
Author: User
Node is a Javascript runtime environment ). In fact, it encapsulates the GoogleV8 engine. The V8 engine executes Javascript very quickly and performs very well. Node has optimized some special use cases and provided alternative APIs, so that V8 can run better in a non-browser environment in the ubuntu system environment and simply record the process.

First install the database with apt-get, type the command sudo apt-get install mysql-server, press enter all the way, and then set the Database root Password on a page.

We need to create something in the database. Type mysql-uroot-p ×××× to enter the SQL console.

1. create database databasename first;

2. use database databasename;

3. Create a table (it is very easy to create here, only the auto-incremental id, user name and password)

 create table user_info (                             id int(11) not null auto_increment,   username varchar(30) not null,   password varchar(30) not null,   primary key ( id )  )ENGINE=InnoDB DEFAULT CHARSET=utf8 ;

4. insert an insert into user_info values (1, 'mtjss2', '123 ');

Below is the node Section

Create a folder, such as myapp. After npm init is in it, the node_modules dependent folder will also be created.

Use npm install -- save to install the following dependencies:

1. express

2. mysql

3. body-parser (used to parse the post parameter)

Because express does not seem to have its own database encapsulation, it generally creates a new models folder and then writes the database logic. If you just want to perform a simple test, you can directly write it in app. js.

For example, in models, There is a logic encapsulation class of user. js. Currently, there is only one method to obtain user information through username. Other methods can be added later.

var mysql = require('mysql') ;                                                                                        //connection config  var connection = mysql.createConnection({    host : 'localhost' ,    user : 'root' ,    password : '123456' ,    database : 'my_box' }); function User(user){   this.username = user.username ;   this.password = user.password ; } User.getUserbyUsername = function(username,callback){   var selectSql = 'select * from user_info where username = ?' ;   connection.query(selectSql,[username],function(err,res){   ¦  if(err){   ¦  ¦  console.log('getUserbyUsername err:' + err) ;   ¦  ¦  return ;   ¦  }   ¦  console.log('Get name success') ;   ¦  callback(err,res) ;   }) ; } ; module.exports = User ;

Paste the app. js and do not write the route temporarily, that is, the processing of the // and/reg methods.

 var mysql = require('mysql') ;                       //connection config  var connection = mysql.createConnection({    host : 'localhost' ,    user : 'root' ,    password : '123456' ,    database : 'my_box'  }); function User(user){   this.username = user.username ;   this.password = user.password ; } User.getUserbyUsername = function(username,callback){   var selectSql = 'select * from user_info where username = ?' ;   connection.query(selectSql,[username],function(err,res){   ¦  if(err){   ¦  ¦  console.log('getUserbyUsername err:' + err) ;   ¦  ¦  return ;   ¦  }   ¦  console.log('Get name success') ;   ¦  callback(err,res) ;   }) ; } ; module.exports = User ;

The homepage index.html is in the views folder. There is no code, but I will post it as well.

  my box  

In this way, access localhost: 3000 in the browser after the node app. js is started. Enter the username in the database in the input file to obtain the data.

Ps: What are the advantages and disadvantages of using Node. js?

Advantages of Node. js:1. It adopts event-driven and asynchronous programming and is designed for network services. In fact, the anonymous functions and closure features of Javascript are very suitable for event-driven and asynchronous programming. JavaScript is also easy to learn. Many front-end designers can quickly get started with backend design. 2. Node. js non-blocking mode IO processing to Node. js brings high performance and outstanding load capabilities under relatively low system resource consumption, and is very suitable for use as an intermediate layer service dependent on other IO resources. 3. Node. js is lightweight and efficient. It can be considered as a perfect solution for real-time application systems in a data-intensive distributed deployment environment. Node is very suitable for the following situations: you may expect high traffic before responding to the client, but the server logic and processing required are not necessarily great.

Disadvantages of Node. js:1. low reliability 2. Single-process, single-thread, only single-core CPU is supported, and multi-core CPU servers cannot be fully utilized. Once this process crashes, the entire web service will crash.

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.