Home >Web Front-end >JS Tutorial >Summarize the common modules of Node.js

Summarize the common modules of Node.js

零到壹度
零到壹度Original
2018-04-12 13:51:381531browse


The content of this article is to summarize the common modules of Node.js. It has certain reference value. Friends in need can refer to it

Node.js native module

querystring


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' }

url

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 :// /;?#

## Example:

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)


Function: Same as parsing, but the query string can be accessed through obj.query.xx


Example:

var url = require("url");
var u = " 
var obj = url.parse(u,true);
var uname = obj.query.uname;console.log(uname);
//lilei
fs


Function: Manipulate files or directories in the file system

Quote:

var fs = require("fs");


Commonly used API:

In blocking state:

var data = fs.readFileSync(fileName); Function: read the file

fs.writeFileSync(fileName,str /buf); Function: Write to file

fs.appendFileSync(fileName,str/buf); Function: Append string into file

Example:

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:


FS.Readfile (FILENAME, (Er, DATA) = & GT; {}); read the file content


# Fs.writeFile (FILENAME, (Er, DATA) = & GT ;{}); Write to file


fs.appendFile(fileName,(err,data)=>{});Append to write file


Example:


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 module

Function: 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服务器接收到相应");
})
----------------------- -------------------------------------------------- ----------------------------------------

Third-party module

mysql

Introduction:

const mysql = require("mysql");


Use case:

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


How to use

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!

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