Home  >  Article  >  Web Front-end  >  node.js operates mysql (add, delete, modify, check)_node.js

node.js operates mysql (add, delete, modify, check)_node.js

WBOY
WBOYOriginal
2016-05-16 15:49:161991browse

I feel good about studying Node recently. I made an add, delete, modify and check myself. Although it is a bit crude, the idea is clear. In fact, all projects are add, delete, modify and check, which helps beginners quickly master Node

First of all

This example shows a set of additions, deletions, modifications and queries quickly built based on Node Express node-mysql. The view template is jade, which is basically a technology that can be used now. There are very few examples on the market. It is useful. It’s not new, so I wrote one myself

Basic work

First of all, we prepare some basic things. Because I use mysql, I can install mysql myself. I can download installation packages for various operating systems from the official website.

The example is just one table. The following is the table creation statement for this table

 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

npm install installs the toolkit required for the project. Now configure the database connection. Configuring the database connection in node is very simple and only requires the following code. It is not like importing the jar package in java and then writing a set of connection management classes. It can be said to break people’s hearts

var mysql = require("mysql");

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

Here host is the current IP address of your computer, user is the username, password is the password, and database is the database to be operated. If you reach this step, you can open the server and try adding, deleting, modifying and checking

The structure of the project (helps to quickly understand the project)

-action is used for the ajax interface provided by the server to the client browser
-mysqlDB.js is used to provide user.js with tool classes for linking to the database and classes for actually operating the database
-user.js is used to provide the implementation layer with methods for adding, deleting, modifying, and checking users. It is equivalent to an intermediate layer
-node_modules is the dependency package required by nodejs
-public provides front-end css and js
-routes provides routes
-views provides view templates
-The startup port and entrance of the app.js program

I have made simple annotations in other places for your convenience

Under the same project package, there are source codes of related nodes and a blog case source code. Of course, I did not refer to the blog source code to implement my program. Well, that’s basically it. I wish everyone good luck

Welcome to my GIT to grab the source code, click Repositories to view Nodehttps://github.com/Mrxdh

If you like it, you can help click Follower

If it is helpful, please click and recommend!

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn