Today continues yesterday's tutorial, yesterday has completed the express installation and the image upload function, I believe that you crossing harvest quite abundant, have what question please direct point, welcome everybody to correct ^_^.
Database such as title, using open source MySQL as the basis, I am downloading the decompression version (Baidu on its own, with Baidu download on the line), the configuration process http://blog.csdn.net/ccf19881030/article/details/9247235,
I in the installation process, for the configuration My.ini file really looked for a long time tutorial, so paste my My.ini file
[Mysqld]basedir= "D:/mysql" datadir= "D:/mysql/data"= 3306= "/tmp/ Mysql.sock "== 3306="/tmp/mysql.sock "default
You can see that my MySQL is installed in the MySQL folder, you can follow your own path to modify.
And then run it under the Bin folder
Mysqld-install
Install the MySQL service, and then start the service.
net start MySQL
The above configuration MySQL tutorial link has navicat for MySQL (is the MySQL visualizer), there are registered information, do not want to hit the command line of the students can download the use, more convenient.
First Login Database
Mysql-uroot-p
Direct enter, no password.
(PS: When I manually fill in the database in the use of the Chinese language will have garbled problems, the solution is as follows: Right-click a database, and then select Database properties, modify the character set to UTF8 format, organize and select the first one on the line,
)
You will use node to connect to the database below. Everyone crossing can make a cup of tea to continue ~.
Felixge/node-mysql is a pure nodejs MySQL client program implemented with JavaScript. The Felixge/node-mysql encapsulates the basic operation of Nodejs for MySQL, 100% mit Public License.
Project Address: Https://github.com/felixge/node-mysql
Install Node-mysql in the project, enter the MyApp folder that was created yesterday and run:
NPM install MySQL
Next Test, the official website to modify the example, put in our project, modify Routes/index.js, restart Express
varExpress = require (' Express ');varRouter =Express. Router ();varMySQL = require (' mysql ');//call the MySQL module
Router.get (‘/‘,function(req, res) {Res.render (' Index ', {title:' Meng Soul ' }); //Create a connection varConnection =mysql.createconnection ({host:' 127.0.0.1 ',//HostUser: ' Root ',//MySQL authenticated user namePassword: ' 111 ',//MySQL authentication user password, no test no password when empty can login, can not set the login passwordPort: ' 3306 ',//Port numberDatabase: ' Nodesample ' }); //Create a connectionConnection.connect (function(err) {if(Err) {Console.log (' [query]-: ' +err); return; } console.log (' [Connection connect] succeed! '); }); //Execute SQL statementConnection.query (' SELECT 1 + 1 as solution ',function(Err, rows, fields) {if(Err) {Console.log (' [query]-: ' +err); return; } console.log (' The solution is: ', rows[0].solution); }); //Close ConnectionConnection.end (function(err) {if(err) {return; } console.log (' [Connection end] succeed! '); });});
Module.exports = router;
When the page is opened, the command appears as follows:
Test success!! , build the test database below
CREATE DATABASE IF not EXISTS nodesample CHARACTER SET UTF8; Use Nodesample; SET foreign_key_checks=0;D rop TABLE IF EXISTS ' userinfo '; CREATE TABLE ' userinfo ' ( int(one) not NULL auto_increment COMMENT ' primary key ', ' UserName ' varchar ( C4>64) NOT null COMMENT ' user name ', ' userpass ' varchar' NOT null COMMENT ' user password ', PRIMARY KEY (' Id ')) EN Gine=innodb DEFAULT charset=utf8 comment= ' user Information table ';
This code can be run directly inside the navicat , click the tool, console, paste, enter the good.
Next, add a piece of data to the database and modify the Routes/index.js, as follows
varExpress = require (' Express ');varRouter =Express. Router ();varMySQL = require (' mysql ');//call the MySQL moduleRouter.get (‘/‘,function(req, res) {Res.render (' Index ', {title:' Meng Soul ' }); //Create a connection varConnection =mysql.createconnection ({host:' 127.0.0.1 ',//HostUser: ' Root ',//MySQL authenticated user namePassword: ' 111 ',//MySQL authenticated user passwordPort: ' 3306 ',//Port numberDatabase: ' Nodesample ' }); //Create a connectionConnection.connect (function(err) {if(Err) {Console.log (' [query]-: ' +err); return; } console.log (' [Connection connect] succeed! '); }); //Execute SQL statement varUseraddsql = ' INSERT into UserInfo (id,username,userpass) VALUES (0,?,?) '; varUseraddsql_params = [' Wilson ', ' ABCD ']; //IncreaseConnection.query (Useraddsql, Useraddsql_params,function(err, result) {if(Err) {Console.log (' [INSERT ERROR]-', Err.message); return; } console.log ('--------------------------INSERT----------------------------'); //console.log (' INSERT ID: ', Result.insertid); Console.log (' INSERT ID: '), result); Console.log ('-----------------------------------------------------------------\ n '); }); //Close ConnectionConnection.end (function(err) {if(err) {return; } console.log (' [Connection end] succeed! '); });}); Module.exports= Router;
Restart Express, refresh the page, the command symbol displays:
The database displays:
OK, now everyone can operate the database, some basic operations please refer to Http://www.cnblogs.com/zhongweiv/p/nodejs_mysql.html#mysql_mod, adding and removing changes inside are introduced.
The following should be introduced to upload pictures of the storage database, but encountered a number of pits, just solve, the most wonderful stay tomorrow ~,
Mainly the problem of routing, home page request template, this time is unable to send data to the front end, so will use the Express middleware, everyone goodnight ~.
Express implementation of front-end communication upload pictures, storage database (MySQL) fool tutorial (ii)