Node. js operates mysql (add, delete, modify, and query) and node. jsmysql

Source: Internet
Author: User

Node. js operates mysql (add, delete, modify, and query) and node. jsmysql

Recently, I have been studying Node for a while. Although it is a bit simple, I think it is a good idea. In fact, all projects are addition, deletion, modification, and query, which helps beginners quickly master Node

First

This example shows a set of add, delete, modify, and query operations quickly built based on Node + Express + node-mysql. The view template is jade, basically all of which are currently usable technologies, there are also few instances on the market, so you can write

Basic work

First, we have prepared some basic information, because I have trouble installing mysql on my own, and I can go to the official website to install installation packages for various operating systems.

The instance is a table. The following table creation statement is used.

 SET NAMES utf8;SET FOREIGN_KEY_CHECKS = 0-- ------------------------------ Table structure for `user`-- ----------------------------DROP TABLE IF EXISTS `user`;CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(255) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, `age` int(4) DEFAULT NULL, `info` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;SET FOREIGN_KEY_CHECKS = 1;

Go to GIT to download my project

The toolkit required for npm install to install the project. Configure the database connection below. It is quite simple to configure the database connection in node by using the following code, unlike importing a jar package in java and then writing a set of connection management classes, this is really broken down.

var mysql = require("mysql");var connection = mysql.createConnection({  host: 'localhost',  user: 'root',  password: '81527319',  database: 'node_test'});

Here, host is the IP address of your computer, user is the user name, password is the password, and database is the database to be operated. If this step is reached, you can enable the server to try adding, deleting, modifying, and querying.

Project Structure (helps you quickly understand the project)

-Action is the ajax interface provided by the server to the client browser.
-MysqlDB. js is used to provide user. js with the tool class for linking the database and the class for actually operating the database.
-User. js is used to provide the Implementation Layer with an addition, deletion, modification, and query method for users, which is equivalent to an intermediate layer.
-Node_modules is the dependent package required by nodejs.
-Public provides front-end css and js
-Routes provides routes
-Views provides a view template.
-App. js program startup port and entry

In other places, I have made simple comments for your convenience.

I have node source code and blog case source code under the same project package. Of course, I didn't refer to the blog source code to implement my program. I wish you good luck.

Welcome to my GIT to take away the source code. Click Repositories to view Nodehttps: // github.com/Mrxdh

Click Follower if you like.

If it is helpful, click here for recommendations!

The above is all the content of this article. I hope you will like it.

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.