Home >Web Front-end >JS Tutorial >Summarize the common modules of Node.js
Function: Convert query string to object
Quote:
var qs = require("querystring");
Commonly used API:
qs.parse(str);
Example:
const qs = require("querystring"); var url = "uname=lilei&upwd=123456"; var obj = qs.parse(url); console.log(obj);
Output:
{ uname: 'lilei', upwd: '123456' }
Function: Convert each part of the url address into js object attributes
Reference:
var url = require("url");
Commonly used API :
1. var obj = url.parse(u)
Function: resolve the address to
var url = require("url"); var u = " var obj = url.parse(u); console.log(obj);// Url { // protocol: 'http:', // slashes: true, // auth: null, // host: ' // port: null, // hostname: ' // hash: null, // search: '?uname=lilei', // query: 'uname=lilei', // pathname: '/index.html', // path: '/index.html?uname=lilei', // href: 'http://www.baidu.com/index.html?uname=lilei' // }2. var obj = url .parse(u,true)
var url = require("url"); var u = " var obj = url.parse(u,true); var uname = obj.query.uname;console.log(uname); //lileifs
const fs = require("fs"); var str = "这是一段文字"; var fileName = "./index.html"; var data = fs.readFileSync(fileName); console.log(data.toString()); /*<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>欢迎访问Node服务器</h1> </body> </html> */ var fileName = "./name.log"; fs.writeFileSync(fileName,str); var str1 = "这是一段追加文字"; fs.appendFileSync(fileName,str1);What is block? Since Node.js is single-threaded, a program can only wait for another program to finish executing, so this waiting state is called blocking API in non-blocking state:
const fs = require("fs"); var str = "这是一段文字"; var fileName = "./index.html"; fs.readFile(fileName,(err,data)=>{ if(err) throw err; console.log(data.toString()); /*<!DOCTYPE html> <html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>欢迎访问Node服务器</h1> </body> </html> */ }) fs.writeFile(fileName,str,(err)=>{ if(err) throw err; console.log("写入成功"); }) fs.appendFile(fileName,str,(err)=>{ if(err) throw err; console.log("追加成功"); })http moduleFunction: It can be used to write a client program (browser) based on the http protocol, or it can also be used to write a server based on the http protocol Terminal program (apache)Case: Write a web server using http protocol
const http = require("http"); var server = http.createServer(); server.listen(3000); server.on("request",(req,res)=>{ //req对象:客户请求消息 //res对象:服务器响应对象 //解析客户请求消息 //向客户端输出相应消息 console.log("node服务器接收到相应"); })----------------------- -------------------------------------------------- ----------------------------------------
const mysql = require("mysql"); var conn = mysql.createConnection({ host:"127.0.0.1", user:"root", password:"", port:3306, database:"database"}); var sql = "select * from database";conn.query(sql,(err,result)=>{ if(err) throw err; console.log(result); //result 为最终查询结果 });Note: Common security vulnerabilities in databases: sql injection Using special characters # ' or1=1 DROP select info to complete the data intrusion operation node .js solution sql placeholder SELECT aid FROM xz_admin WHERE aname = ? ANDapwd = md5(?) conn.query(sql,["tom" ,"123"]);Use "pool" Function: Improve efficiency and reduce the time required to create and close connections
const mysql = require("mysql"); var pool = mysql.createPoolCluster({ host:"127.0.0.1", user:"root", password:"", port:3306, database:"database", connectionLimit:5 }) var sql = "select * from database"; pool.query(sql,(err,result)=>{ if(err) throw err; console.log(result); //result 为最终查询结果 });
The above is the detailed content of Summarize the common modules of Node.js. For more information, please follow other related articles on the PHP Chinese website!