Express realize front-end back-end communication upload image storage database (MySQL) Fool-type tutorial (ii) _JAVASCRIPT skills

Source: Internet
Author: User
Tags mysql client mysql download mysql tutorial install node

In the previous article to introduce the express to realize the front-end back-end communication upload pictures of the storage database (MySQL) fool tutorial (i)

Database such as the title, using the open source of MySQL as the basis, I am the download of the decompression version (own Baidu has, with Baidu download on the line), the configuration process http://www.jb51.net/article/76206.htm, I in the installation process, For the configuration My.ini file really find a long time tutorial, so put on my My.ini file

[Mysqld]
Basedir= "D:/mysql" 
datadir= "D:/mysql/data" 
port = 3306
socket = "/tmp/mysql.sock"
[client] 
Password = 
Port = 3306
socket = "/tmp/mysql.sock"

You can see that my MySQL is installed in the MySQL folder, you can follow their own path to modify.

Then run under the Bin folder

Mysqld-install

Install the MySQL service, and then start the service.

net start MySQL

The above configuration of the MySQL tutorial link has navicat for MySQL download address (is the visual tool for MySQL), as well as the registration of information, do not want to play the command line students can download the use of more convenient.

  Log in to the database for the first time

Mysql-uroot-p

Direct return, no password.

PS: I use in the manual fill in the database, input Chinese will have garbled problem, the solution is as follows: Right a database, and then select Database properties, modify the character set for UTF8 format, collation Select the first on the line, as shown:

Here you will use node to connect to the database. You reader can make a cup of tea to continue ~.

Felixge/node-mysql is a pure nodejs MySQL client program implemented with JavaScript. Felixge/node-mysql encapsulates Nodejs's basic operations on MySQL, 100% the MIT Public License.

Project Address: Https://github.com/felixge/node-mysql

Install Node-mysql in the project, enter the MyApp folder that was established yesterday, and run:

NPM install MySQL

Next to the test, the official website of the example modified, put in our project, modify Routes/index.js, restart Express

 var Express = require (' Express '); var router = Express. Router (); var mysql = require (' mysql ');
 Call MySQL module router.get ('/', function (req, res) {res.render (' index ', {title: ' Meng Soul '}); Create a connection var connection = Mysql.createconnection ({host: ' 127.0.0.1 ',//Host User: ' Root ',//mysql authenticated username password
 : ' A ',//mysql authenticated user password, no test without password for empty whether can log in, can not be set down login password port: ' 3306 ',//Port number database: ' Nodesample '};
 Create a connection Connection.connect (function (err) {if (err) {console.log (' [query]-: ' + err ');
 Return
 } console.log (' [Connection connect] succeed! ');
 }); Execute SQL statement connection.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);
 });
 Turn off Connection connection.end (function (err) {if (err) {return;
 } console.log (' [Connection end] succeed! ');
});
}); 
Module.exports = router; 

When you open a page, the command character appears as follows:

Test success!! , set up the test database below

CREATE DATABASE IF not EXISTS nodesample CHARACTER SET UTF8;
Use Nodesample;
SET foreign_key_checks=0;
DROP TABLE IF EXISTS ' userinfo ';
CREATE TABLE ' userinfo ' (
 ' Id ' int (one) not NULL auto_increment COMMENT ' primary key ',
 ' UserName ' varchar () NOT NULL comme NT ' username ',
 ' userpass ' varchar not NULL COMMENT ' user password ',
 PRIMARY KEY (' Id ')
engine=innodb DEFAULT charset= UTF8 comment= ' user Information table ';

This code can be run directly in the Navicat, click the tool, console, paste, enter the good.
Next, add a piece of data to the database, modify the Routes/index.js, as follows

var express = require (' Express '); var router = Express.
Router (); var mysql = require (' mysql ');
 Call MySQL module router.get ('/', function (req, res) {res.render (' index ', {title: ' Meng Soul '}); Create a connection var connection = Mysql.createconnection ({host: ' 127.0.0.1 ',//Host User: ' Root ',//mysql authenticated username password
 : '//mysql ', authentication user password port: ' 3306 ',///Port Number database: ' Nodesample '};
 Create a connection Connection.connect (function (err) {if (err) {console.log (' [query]-: ' + err ');
 Return
 } console.log (' [Connection connect] succeed! ');
 });
 Execute SQL statement var useraddsql = ' INSERT into UserInfo (id,username,userpass) VALUES (0,?,?) ';
 var useraddsql_params = [' Wilson ', ' ABCD '];  Add Connection.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 ');
 });
 Turn off Connection connection.end (function (err) {if (err) {return;
 } console.log (' [Connection end] succeed! ');
}); });

Module.exports = router;

Restart Express, Refresh page, command character display:

Database display:

OK, now everyone can operate the database, some basic operations please refer to http://www.cnblogs.com/zhongweiv/p/nodejs_mysql.html, additions and deletions to check inside are introduced.

The back should be introduced to upload the image of the store database, but encountered some pits, only to solve, the most wonderful to stay tomorrow ~,
Mainly the problem of routing, the main page requested the template, this time is not to send data to the front, so will use the express middleware, everyone good night ~.

Related Article

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.