A brief analysis of node connection database (express mysql)_node.js
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 ;
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 ;
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 ;
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>
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...
