当前位置:网站首页>Using MySQL database in the express framework of node
Using MySQL database in the express framework of node
2022-06-28 12:05:00 【Coke loves to stay at home】
Keep creating , Accelerate growth ! This is my participation 「 Nuggets day new plan · 6 Yuegengwen challenge 」 Of the 13 God , Click to see the event details
Related introduction
- Preface
as everyone knows , stay Web Server side development , A very important part of operating a database , Even when the front and back ends tease each other, when the back end says that the front end is a cutaway , The front-end will retort that the background development is a CRUD Child , and CRUD Is the corresponding database Additions and deletions operation , and Mysql Database is one of the most commonly used databases .
- Introduce
Used to say Mysql Database is a relational database management system , Because of its small size 、 Fast 、 Low total cost of ownership , In especial Open source This characteristic , Generally, the development of small and medium-sized websites and large-scale websites all choose MySQL As website database .
install Mysql
To use Mysql Words , First, install in the local environment Mysql Software can only be used , Can be in Official website download address Find the corresponding download link to download the software .
After installing the software , Get into cmd Software input command mysql -V You can see the installed Mysql Software information :
Then perform mysql -uroot -p After entering the password, you can see the entry Mysql Terminal management interface , Here's the picture :
If the above operations can be performed correctly , It means that the installation is correct Mysql The software .
install Navicat
Operate in the terminal Mysql Sometimes it is inconvenient , So it's time to Navicat To support Mysql The visual operation interface . Once installed , You can connect to the local database through software , Here's the picture :
Start using
First , By using express-generator Generator to create a controlled Express project , as follows :
npx express-generator express-mysql
next , Installation operation is required Mysql Database dependency . stay Node Used to be used in mysql Rely on operational databases , Now there is an upgraded version mysql2 You can manipulate the database , The library has been optimized compared with the former , So here's the installation mysql2 rely on , Execute the following code :
npm install --save mysql2
Install well mysql2 After dependence , Create a new one db.js file , This file is used to write operations mysql Configuration file for , The contents are as follows :
const mysql = require('mysql2')
const connection = mysql.createConnection({
host: 'localhost',
port: 3306,
user: 'root',
password: 'password',
database: 'demo'
})
connection.connect()
module.exports = connection
adopt mysql.createConnection You can set up a database connection , Then you can operate through the return object of this function mysql database , The meanings of the configured attributes are as follows :
| attribute | significance |
|---|---|
| host | Connected database hostname , String type |
| port | Connected database port number , value type |
| user | Connected database user name , String type |
| password | The password of the connected database , String type |
| database | Database name of the connection , String type |
After the file configuration , Through the first Navicat Medium demo Database users Add a record to the table , Here's the picture :
Then you can start to operate the database , In routing files users.js in , Try querying a record in the database , And return to the page to display , as follows :
const express = require('express');
const router = express.Router();
const db = require('../utils/db')
/* GET users listing. */
router.get('/', function(req, res, next) {
db.query('select * from users', (err, users) => {
if (err) throw err
res.json(users)
})
});
module.exports = router;
adopt db.query Naming can be done in node In the implementation of mysql The addition, deletion, modification, and query of , Enter the corresponding... In the browser Api After that, you can see the following information :
thus , Has already succeeded in Node Use in Mysql Database :)
边栏推荐
- Necessary for beginners PR 2021 quick start tutorial, PR green screen matting operation method
- Still using simpledateformat for time formatting? Be careful that the project collapses!
- Redis hash hash type string (5)
- Remote login sshd service
- Why do many people want to change careers as programmers, while some programmers want to change careers as others?
- Swin, three degrees! Eth open source VRT: a transformer that refreshes multi domain indicators of video restoration
- day31 js笔记 DOM下 2021.09.26
- Web3安全连载(3) | 深入揭秘NFT钓鱼流程及防范技巧
- Array method in JS 2021.09.18
- Web page tips this site is unsafe solution
猜你喜欢

无法重新声明块范围变量

纯纯大怨种!那些年被劝退的考研专业

For example, the visual appeal of the live broadcast of NBA Finals can be seen like this?

What method is required for word, PDF and txt files to realize full-text content retrieval?

day37 js笔记 运动函数 2021.10.11

Tidb v6.0.0 (DMR): initial test of cache table - tidb Book rush

Packaging and publishing application of jetpack compose desktop version

Cannot redeclare block range variables

day31 js笔记 DOM下 2021.09.26

Is it feasible to be a programmer at the age of 26?
随机推荐
AcWing 606. Average 1 (implemented in C language)
Industry analysis - quick intercom, building intercom
Adding a new user in MySQL 5.7
Redis hash hash type string (5)
recent developments
面试步骤的面试技巧
day23 js笔记 2021.09.14
For example, the visual appeal of the live broadcast of NBA Finals can be seen like this?
Leetcode 705. 设计哈希集合
Open3d manual clipping point cloud
Multi dimensional monitoring: the data base of intelligent monitoring
What method is required for word, PDF and txt files to realize full-text content retrieval?
Day32 JS note event (Part 1) September 27, 2021
ProCAST finite element casting process simulation software
案例驱动 :从入门到掌握Shell编程详细指南
Unity screenshot function
NFT card chain game system development DAPP construction technical details
Difference (one dimension)
day31 js笔记 DOM下 2021.09.26
AcWing 610. Salary and bonus (implemented in C language)