Node.js operation sql detailed steps

1. Why learn node.js

  1. understand nodejs
  2. Understand the backend (read local files, connect to databases, respond to requests)
  3. Understand the api interface document

2. Check whether there is a node and whether mysql is installed successfully

node -v

  • If this happens
    insert image description here
  • Proceed as follows. Find the task manager through ctrl+alt+delete, find mysql in the service and click start
    insert image description here
  • Then perform the following operations mysql -u root -p enter the password
    insert image description here

3. Initialize the project

  1. Change to the project directory
  2. npm init -y

4. Using third-party modules

  1. install modulenpm i axios -S
  2. import moduleconst axios = require("axios")
  3. use modulesaxios.get(url).then(res=>{})

5. Using custom modules

  1. define module utils.js
    module.exports ={max(){},randomStr(){}}
  2. Import and use (Method 1)
    Import const utils = require('./utils.js')
    and use utils.max() utils.randomStr()
    Import and use (Method 2)
    Import and const {max,randomStr} = require(' ./utils.js')
    usemax() randomStr()
  3. quick export
    exports.say = function(){console.log("到结婚了年龄吗?")}

6. Project running

  1. configuration command
package.json->script
"serve":"node main.js"
npm run serve
  1. cmd
    1. Enter the project directory
    2.node main/js

7. mysql command

  1. query select
SELECT * FROM `feedback ` WHERE 1;
//指定列查询 
SELECT 'msg ',' name'  `FROM `feedback` WHERE 1;
//添加查询条件 
SELECT * FROM feedback WHERE name='小曾;
//查询msg中包含山的元素  %代表是任意字符
SELECT * FROM `feedback` where msg like '%%;
//_代表任意一个字符串
select* from feedback where msg like '山_有%';
//desc按时间排序降序
select * from feedback where 1 order by `datetime ' desc;
//查询偏移2个截取3行
select * from feedback where 1 order by `datetime ' desc limit2,3;
  1. increase insert into
  2. delete delete
  3. modify update

8.node operation sql⭐

  1. Installnpm i mysql -S

  2. importconst mysql = require("mysql")

  3. create connectionconst conn = mysql.createConnect({ host:"localhost", user:"root", password:"", ​database:"feed"})

  4. Connect to the database
    conn.connect(function(err){if(!err){console.log("数据库连接成功")}})
    Navicate is needed to connect to the database ⭐
    1. Open the connection and find mysql
    insert image description here

    2. Double-click to open the message board after entering the password successfully.
    insert image description here
    3. Double-click to open the message board after successfully entering the password.
    insert image description here

  5. define sqlvar sql = "select * from feedback where 1”

  6. execute sqlconn.query(sql,function(err,result){if(!err){console.log(result)}})

  7. disconnect databaseconn.end(function(){console.log("数据库己断开")})

9. Built-in server creation ⭐

1. Import http const http = require("http")
2. Create service
const server = http.createServer(function(req,res){ //req请求的数据 //res响应的数据 res.statusCode = 200; //响应码 res.setHeader("Content-Type","application/json") //响应类型 res.end({} )//返回的数据 })
3. Listen port
server.listen(8888,function(){ console.log("localhost:8888 启动") })

Guess you like

Origin blog.csdn.net/m0_55734030/article/details/127112353