The operation is in the Ubuntu system under the environment, a simple record of the process.
First use Apt-get to install the database, type the command sudo apt-get install mysql-server, all the way to enter, and then set the database root password in one interface.
In the database we need to create something. Type mysql-uroot-pxxxxx to enter the SQL console.
1. First create the dataset creating database DatabaseName;
2.use database databasename;
3. Create a table (here is a very simple one, only the ID, username and password)
CREATE TABLE user_info (
ID int (one) not NULL auto_increment,
username varchar (a) NOT NULL,
password varchar ( Not NULL,
primary key (ID)
) engine=innodb DEFAULT Charset=utf8;
4. Insert a piece of data into user_info values (1, ' mtjss2 ', ' 123456 ');
Here's a section of node
Create a folder, such as MyApp, which, after NPM Init, will also node_modules dependent folders
Install the following dependencies with NPM install--save
1.express
2.mysql
3.body-parser (used to parse post parameters)
Because Express does not seem to have its own database encapsulation, so generally will create a new models folder, and then write the logic of the database, if just want to simple test can be written directly in App.js
Here for example under models there is a User.js logic package class, temporarily only one through the username to obtain user information, the other 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 =? '; C14/>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;
Just a little bit more. App.js is temporarily not write route, is the processing of/and/reg method
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 =? '; C14/>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;
Index.html This main page in the Views folder, no code, but also posted
So use node app.js to start in the browser to access the localhost:3000 on the line, input in the database in the user name can get that data.
PS: What are the advantages and disadvantages of using node.js?
Node.js Advantages:1, the use of event-driven, asynchronous programming for network services and design. In fact, JavaScript's anonymous functions and closure features are ideal for event-driven, asynchronous programming. And JavaScript is easy to learn, many front-end designers can quickly start to do back-end design. 2. Node.js non-blocking Mode IO processing provides Node.js with high performance and superior load capability under relatively low system resource consumption, and is ideal for use as a middle tier service that relies on other IO resources. 3, Node.js lightweight and efficient, can be considered as a data-intensive distributed deployment environment of real-time application system perfect solution. node is ideal for situations where you anticipate a high flow of traffic before responding to a client, but not necessarily the server-side logic and processing required.
node.js Disadvantage:1, low reliability 2, single process, single-threaded, only support single core CPU, can not fully utilize Multi-Core CPU server. Once the process collapses, the entire Web service is blown off.