Home > Web Front-end > JS Tutorial > A brief analysis of node connection database (express mysql)_node.js

A brief analysis of node connection database (express mysql)_node.js

WBOY
Release: 2016-05-16 15:28:51
Original
2471 people have browsed it

The operation is under the environment of ubuntu system, and the process will be simply recorded.

First use apt-get to install the database, type the command sudo apt-get install mysql-server, press Enter all the way, and then set the database root password on an interface

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

1. First create a data set create database databasename;

2.use database databasename ;

3. Create a table (create a very simple one here, only auto-increment id, username 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 ;
Copy after login

4. Insert a piece of data insert into user_info values(1,'mtjss2','123456') ;

The following is the part of node

Create a folder, such as myapp, and after npm init, the dependency folder of node_modules will be included

Use npm install --save to install the following dependencies

1.express

2.mysql

3.body-parser (used to parse post parameters)

Since Express does not seem to have its own database package, you usually create a new models folder and then write the logic of the database. If you just want a simple test, you can write it directly in app.js

For example, here is a logical encapsulation class of user.js under models. Currently, there is only one method to obtain user information through username. Others 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 ;
Copy after login

Post again app.js does not write routing for the time being, it is the processing of / 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 ;
Copy after login

The main page index.html is under the views folder. There is no code, but I will post it here

<!DOCTYPE html>
<html>
<head>
  <title>my box</title>
</head>
<body>
  <form action="/reg" method="post">
    <input type="text" name="username"/>
    <input type="submit" value="submit"/>
  </form>
</body>
</html>
Copy after login

After starting with node app.js, just access localhost:3000 in the browser. Enter the user name in the database in the input to get the data.

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

Node.js advantages: 1. It uses event-driven, asynchronous programming and is designed for network services. In fact, Javascript's anonymous functions and closure features are very suitable for event-driven and asynchronous programming. Moreover, JavaScript is easy to learn, and many front-end designers can quickly start doing back-end design. 2. Node.js' non-blocking mode IO processing brings Node.js high performance and outstanding load capacity with relatively low system resource consumption, making it very suitable for use as a middle-tier service that relies on other IO resources. 3. Node.js is lightweight and efficient, and can be considered a perfect solution for real-time application systems in data-intensive distributed deployment environments. Node is great for situations where you anticipate there may be high traffic before responding to the client, but the required server-side logic and processing isn't necessarily a lot.

Node.js Disadvantages: 1. Low reliability 2. Single process, single thread, only supports single-core CPU, and cannot fully utilize multi-core CPU servers. Once this process crashes, the entire web service crashes.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template