Home > Web Front-end > JS Tutorial > body text

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)

青灯夜游
Release: 2023-01-13 20:23:18
forward
20580 people have browsed it

This article uses a lot of code and illustrations to give you an in-depth analysis of Node.js. The main content includes modular processing, basic application of packages, Express, cross-domain, operating Mysql database, etc. I hope it will be helpful to everyone!

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text). Introduction to Node.jsarticle will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)>This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) What is Node.jsarticle will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)>

Node.js is a js that calls the built-in ApI and is based on the Chrome V8 engine. Regarding the environment, I have summarized some scattered knowledge points locally before, and I will integrate them and send them out today.

Official website address: https://nodejs.org/zh-cn/

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) What can Node.js doarticle will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)>

① Based on the Express framework (http://www.expressjs.com.cn/), Web applications can be quickly built. [Related tutorial recommendations: nodejs video tutorial, Programming teaching]

②Based on the Electron framework (https://electronjs.org/) , you can build cross-platform desktop applications

③Based on the restify framework (http://restify.com/), you can quickly build API interface projects

④Read and write And operate database, create practical command line tools to assist front-end development, etc...

##This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) Node.js installationarticle will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)>

Download link: https://nodejs.org/en/

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)

    ##LTS
  • : Long-term stable version
  • Current
  • :Early adopter version
View version number

:node –v

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)

Learning route: JavaScript basic syntax Node.js built-in API modules (fs, path, http, etc.) Third-party API modules (express, mysql, etc.)

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).4 Use of Node.jsarticle will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)>Command

: node js file name

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)

Terminal Shortcut keys

: ①Use the ↑ key to quickly locate the last executed command

②Use the tab key to quickly complete the path

③Use The esc key can quickly clear the currently entered commands

④Enter the cls command to clear the terminal

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text). Modular processingarticle will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)>This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) What is modularityarticle will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)>

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)##Definition

: Simplify complex problems and divide them into small problems.

Modularization in the field of programming is to obey fixed rules and split a large file into multiple small modules# that are independent and interdependent ##The benefits of modularizing the code:

Improves the reusability of the code

Improves the reusability of the code Maintainability
  • can be loaded on demand
This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) Built-in modulesDefinition: Officially provided by Node.js, such as article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)>fs, http, path

##This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) fs file system module

(This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)) Basic usage
// 引用内部模块
const fs = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;fs&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);

// 文件读取
fs.readFile(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;../files/test-fs.txt&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;utf-8&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, (err, results) => {
    if (err) return console.log(err.message);// 错误信息err null
    console.log(results);
})

// 文件写入
fs.writeFile(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;../files/test-fs.txt&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;Node.js&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;utf-8&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, (err) => {
    if (err) return console.log(err.message);
    console.log(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;写入文件成功!&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
})
Copy after login

Note

readFilecan only read existing files The file

  • writeFile already has a file to write, then create a file with the same name, and then write the file
  • readFile needs to be written in writeFile
  • Read later, otherwise an error will occur
  • (This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)) Prevent dynamic splicing
## The #node

command automatically splices the current path and the This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)js script file path

regardless of
    .\day always review this path
  • We can use Absolute pathImprovement
(This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)) Path problem

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)

./ represents the current directory ../
represents the parent directory
    ../..
  • represents the grandfather directory is dynamically spliced, and the header cannot appear ./ . ./, otherwise splicing fails /…/
  • ##This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) path built-in module

Definition: SplicingAbsolute path
  • path.join()
  • path.basename()
  • path.extname()
const fs = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;fs&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);

const path = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;path&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);

const fpath = path.join(__dirname, &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;/../files/test-fs.txt&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);

fs.readFile(fpath, &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;utf-8&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, (err, results) => {
    console.log(__dirname);
    console.log(path.basename(fpath, &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;.txt&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;));
    console.log(path.extname(fpath));

    if (err) return console.log(err.message);
    console.log(results);
})
// test-fs
// .txt
// Node.js
Copy after login

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) http内置模块

定义Node.js提供创建web服务器

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)

(This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)) 初始化
// 导入http模块
const http = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;http&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);

//创建web服务器实例
const server = http.createServer();

//绑定request事件,监听客户端请求
server.on(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;request&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, (req, res) => {
    let str = `路径 ${req.url} 方法 ${req.method}`;
    console.log(str);
    // 向客户端发送中文前,设置响应头
    res.setHeader(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;Content-Type&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;text/html;charset=utf-8&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
    res.end(str);
})


//启动服务器
server.listen(80, () => {
    console.log(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;http://This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)7.0.0.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
})
Copy after login
(This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)) web服务器

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)

  • 根据浏览器访问的url地址不同,返回相应的绝对路径
const fs = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;fs&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
const http = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;http&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
const path = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;path&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
const server = http.createServer();

let fpath = &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;;
server.on(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;request&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, (req, res) => {
    if (req.url === &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;/&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;) {
        fpath = path.join(__dirname + &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;/../files/clock/index.html&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
        console.log(__dirname);
        console.log(fpath);
    }
    else {
        fpath = path.join(__dirname + &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;/../files/clock&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9; + req.url);
    }
    fs.readFile(fpath, &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;utf-8&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, (err, results) => {
        if (err) res.end(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;404 not find&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
        res.end(results);
    })
})

server.listen(80, () => {
    console.log(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;http://This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)7.0.0.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
})
Copy after login
This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) 自定义模块

定义:用户自定义的js模块

//引入本地文件
const custom = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;./0This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)-node.js的使用&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
Copy after login

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)

注意:自定义模块开头必须有./ …/

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).4 外部模块

定义:由第三方提供,使用前需要下载

//下载外部导入
const moment = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;moment&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
Copy after login

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)

监听nodemon

npm i nodemon -g
Copy after login

代替node使用nodedmon每次修改内容不需要重启服务器,自动监听

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).5 模块化处理

模块作用域定义:和函数一致,当前模块定义的方法、变量,只能在当前模块访问,防止变量污染

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)

暴露:通过module.exports或者exports暴露出去,使用 require() 方法导入模块时,导入的结果,永远以module.exports 指向的对象为准

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).6 加载机制

定义一次加载缓存,从缓存加载 内置模块加载优先级MAX

三、包的基本应用

:概念像node.js的第三方模块,包是基于内置模块封装出来的,提供了更高级、更方便的 API,极大的提高了开发效率

npm: 包管理工具

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) 使用流程
  • npm安装包
  • js导入包
  • 根据开发文档使用包
// npm i moment
const moment = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;moment&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
const date = moment().format(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;YYYY-MM-DD HH:mm:ss&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
console.log(date);//This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)0This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)-09-This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)0 This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)0:4This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text):This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)4
Copy after login
This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) 版本问题

包的版本号是以“点分十进制”形式进行定义的,总共有三位数字,例如 This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)4.0
其中每一位数字所代表的的含义如下:

  • 第This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)位数字:大版本

  • 第This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)位数字:功能版本

  • 第This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)位数字:Bug修复版本

    版本号提升的规则:只要前面的版本号增长了,则后面的版本号归零。

npm i comment@This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)
Copy after login
This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) 参数问题

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)

  • node_modules 文件夹用来存放所有已安装到项目中的包。require() 导入第三方包时,就是从这个目录中查找并加载包。
  • package-lock.json 配置文件用来记录 node_modules 目录下的每一个包的下载信息,例如包的名字、版本号、下载地址等。
  • package.json项目的名称、版本号、描述等、用到了哪些包、开发期间使用的包、部署使用的包
    • devDependencies :开发依赖
    • dependencies :核心依赖
  • 注意:程序员不要手动修改 node_modules 或 package-lock.json 文件中的任何代码,npm 包管理工具会自动维护它们,今后在项目开发中,一定要把 node_modules 文件夹,添加到 .gitignore 忽略文件中
This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).4 npm命令
//安装包 
npm i moment
//安装全局包
npm i 包名 -g
//安装包到开发阶段到devDependencies
npm i 包名 -D
//安装所有依赖包 
npm install
//卸载包 
npm uninstall moment
//查看已经安装的局部包
npm ls
//查看全局安装的包
npm ls -g
Copy after login

查看包命令:https://blog.csdn.net/qq_4This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)664096/article/details/This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)797This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)60

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).5 下载镜像

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)

//查看当前npm镜像
npm config get registry
//nrm镜像工具,安装为全局镜像
nrm ls
//切换镜像
nrm use taobao
Copy after login

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).6 开发自己的包

一个规范的包,它的组成结构,必须符合以下 This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) 点要求:

  • 包必须以单独的目录而存在
  • 包的顶级目录下要必须包含 package.json 这个包管理配置文件
  • package.json 中必须包含 name,version,main 这三个属性,分别代表包的名字、版本号、包的入口

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)

发布包到npm

  • 镜像切换到npm上
  • npm login登录
  • 发布包 npm publish
  • 删除包 npm unpublish 包名 --force

资源

  • https://www.npmjs.com/ 网站上搜索自己所需要的包
  • https://registry.npmjs.org/ 服务器上下载自己需要的包
四、Express4.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) 简介

Express:基于Node.js http进一步封装,更加高级的Web开发框架

对于前端程序员来说,最常见的两种服务器,分别是:

  • Web 网站服务器:专门对外提供 Web 网页资源的服务器
  • API 接口服务器:专门对外提供 API 接口的服务器
4.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) 基本使用
//导入包
const express = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;express&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
//创建服务器
const app = express();

app.get(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;/user&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, (req, res) => {
    res.send({ 男: &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)8&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, age: This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)8 });
})

app.post(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;/user&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, (req, res) => {
    res.send(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;post请求&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
})

app.get(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;/&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, (req, res) => {
    //req.query  ?name=zs&age=This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)8  这种数据
    //http://This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)7.0.0.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)?name=zs&age=This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)8
    console.log(req.query);
})
app.post(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;/:id&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, (req, res) => {
    //动态匹配参数
    console.log(req.params);
})

//启动服务器
app.listen(80, () => {
    console.log(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;http://This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)7.0.0.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
})
Copy after login
4.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) 托管静态资源

定义通过路径暴露文件,省去文件路径的描写

const express = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;express&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);

const app = express();

//托管静态资源,不需要访问
app.use(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;/public&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, express.static(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;../files/clock&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;));

app.listen(80, () => {
    console.log(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;http://This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)7.0.0.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
})
Copy after login

推荐VScode插件:postcode

Express 的中文官网: http://www.expressjs.com.cn/

4.4 路由

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)

定义:客户端与服务器映射关系

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)

4.4.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) 简单挂载

//导入包
const express = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;express&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
//创建服务器
const app = express();

app.get(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;/user&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, (req, res) => {
    res.send({ 男: &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)8&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, age: This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)8 });
})

app.post(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;/user&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, (req, res) => {
    res.send(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;post请求&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
})
//启动服务器
app.listen(80, () => {
    console.log(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;http://This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)7.0.0.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
})
Copy after login

4.4.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) 模块化路由

为了方便对路由进行模块化的管理,Express 不建议将路由直接挂载到 app 上,而是推荐将路由抽离为单独的模块

将路由抽离为单独模块的步骤如下:

  • 创建路由模块对应的 .js 文件

  • 调用express.Router()函数创建路由对象

  • 向路由对象上挂载具体的路由

  • 使用 module.exports 向外共享路由对象

  • 使用app.use()函数注册路由模块

创建路由对象

const express = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;express&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);//导入包

const router = express.Router();//创建路由对象

//绑定路由规则
router.get(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;/user/list&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, (req, res) => {
    res.send(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;user list message&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
})

router.post(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;/user/add&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, (req, res) => {
    res.send(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;user add message&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
})

//向外导出路由对象
module.exports = router;
Copy after login

使用路由对象

const express = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;express&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
const app = express();

const router = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;./This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)-模块化路由&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);

app.use(router);

app.listen(80, () => {
    console.log(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;http://This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)7.0.0.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
})
Copy after login
4.5 中间件

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)

中间件:与路由处理函数不同,必须包含next参数

4.5.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) 基本使用

const express = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;express&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
const app = express();
//全局中间件的简化形式
app.use((req, res, next) => {
    console.log(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;正在使用全局中间件&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
    next();
});

app.get(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;/&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;,(req, res) => {
    res.send(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;Get message&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
})


app.listen(80, () => {
    console.log(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;http://This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)7.0.0.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
})
Copy after login

注意

  • 多个中间件共享req,res,上游设置好,下游的中间件/路由使用

  • 中间件定义先后顺序执行

  • 局部生效的中间件,定义在

    • app.get(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;/&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;,中间件,(req, res) => {
          res.send(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;Get message&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
      })
      Copy after login
    • This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)

  • 路由之前调用中间件

  • next()函数不能忘,后面不用写内容

4.5.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) 中间件分类

(This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text))应用
const express = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;express&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
const app = express();

//全局中间件
app.use((req, res, next) => {
    console.log(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;全局中间件&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
    next();
})
//局部中间件
function mw(req, res, next) {
    console.log(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;局部中间件&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
    next();
}

app.get(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;/&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, mw, (req, res) => {
    res.send(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;server is visting&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
})
app.listen(80, () => {
    console.log(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;http://This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)7.0.0.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
})
Copy after login
(This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text))路由

定义:绑定到 express.Router() 实例上的中间件

(This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text))错误

定义捕获项目错误,防止出错,在所有路由之后定义

const express = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;express&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
const app = express();


app.get(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;/&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, (req, res) => {
    throw new Error(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;服务器出错&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
    res.send(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;server is visting&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
})


//全局中间件
app.use((err, req, res, next) => {
    console.log(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;Error!&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9; + err.message);
    res.send(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;Error!&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9; + err.message);
    next();
})

app.listen(80, () => {
    console.log(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;http://This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)7.0.0.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
})
//Error!服务器出错
Copy after login
(4)Express 内置

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)

const express = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;express&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
const app = express();

// express.json()解析JSON请求体
app.use(express.json());

//解析application/x-www-
app.use(express.urlencoded({ extended: false }));

app.post(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;/user&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, (req, res) => {
    console.log(req.body);
})

app.post(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;/book&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, (req, res) => {
    console.log(req.body);
})

app.listen(80, () => {
    console.log(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;http://This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)7.0.0.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
})
// http://This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)7.0.0.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)
// { name: &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;zs&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, age: This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)8 }
// [Object: null prototype] { name: &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;西游记&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9; }
Copy after login
(5)第三方
  • npm install body-parse
  • require导入
  • app.use()为全局
const express = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;express&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
const app = express();

const parser = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;body-parser&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);

app.use(parser.urlencoded({ extended: false }));

app.post(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;/book&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, (req, res) => {
    console.log(req.body);
})

app.listen(80, () => {
    console.log(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;http://This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)7.0.0.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
})
Copy after login

注意:Express 内置的 express.urlencoded 中间件,就是基于 body-parser 这个第三方中间件进一步封装出来的。

4.6 自定义中间件

封装中间件

const querystring = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;querystring&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);

function parsebody(req, res, next) {
    let str = &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;;
    req.on(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;data&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, (result) => {
        str += result;
    })
    req.on(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;end&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, () => {
        const body = querystring.parse(str);
        req.body = body;
        next();
    })
}

module.exports = parsebody;
Copy after login

测试中间件

const express = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;express&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
const app = express();
const parsebody = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;./This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)4-自定义中间件&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);

app.use(parsebody);

app.post(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;/user&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, (req, res) => {
    res.send(req.body);
    console.log(req.body);

})

app.listen(80, () => {
    console.log(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;http://This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)7.0.0.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
})
Copy after login
4.7 接口
const express = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;express&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
const app = express();

const router = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;./This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)5-接口问题&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
app.use(router);

app.listen(80, () => {
    console.log(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;http://This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)7.0.0.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
})
Copy after login

4.7.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) GET接口

const express = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;express&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);

const apiRouter = express.Router();

apiRouter.get(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;/user&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, (req, res) => {
    const query = req.query;
    res.send({
        status: 0,
        msg: &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;GET 请求成功&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;,
        data: query
    });
})
module.exports = apiRouter;
Copy after login

4.7.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) POST接口

apiRouter.use(express.urlencoded({ extended: false }));

apiRouter.post(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;/user&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, (req, res) => {
    const body = req.body;
    res.send({
        status: 0,
        msg: &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;POST 请求成功&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;,
        data: body
    });
})
Copy after login

4.7.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) PUT接口

4.7.4 DELETE接口

4.7.5 区别

https://blog.csdn.net/qq_4This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)85/article/details/This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)985This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)94

https://zhuanlan.zhihu.com/p/This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)5454697

五、跨域5.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) CORS

5.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) 原理

概念:由Http响应头构成,决定浏览器是否阻止js代码获取资源,在服务器端配置

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)

5.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) 响应头

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)

//只允许特定的域名访问、*代表全部
    res.setHeader(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;Access-Control-Allow-Origin&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;http://www.baidu.com&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
//配置请求头信息
    res.setHeader(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;Access-Control-Allow-Headers&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;Content-Type,X-Custom-Header&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
//配置请求头方法 * 代表全部
    res.setHeader(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;Access-Control-Allow-Methods&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;GET,POST,DELETE,PUT&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
Copy after login

5.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) 分类

(This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text))简单请求
  • 请求方式:GET、POST、HEAD
  • HTTP 头部信息不超过以下几种字段无自定义头部字段、Accept、Accept-Language、Content-Language、DPR、Downlink、Save-Data、Viewport-Width、Width 、Content-Type(只有三个值application/x-www-form-urlencoded、multipart/form-data、text/plain)
  • 客户端与服务器只发送一次请求
(This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text))预检请求
  • 请求方式:GET、POST、HEAD之外的方式
  • 自定义头部字段
  • OPTION预检,成功后发送带有数据的请求
5.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) JSONP

概念:只支持GET请求

六、Mysql数据库

定义组织存储管理数据仓库

6.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) SQL命令

6.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) 查询

select * from userswhere id>This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) and id <5
Copy after login

6.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) 插入

insert into users(username,password) values(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;jack&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;,&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;666&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;)
Copy after login

6.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) 更新

update users set password=&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;666666&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;where username=&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;jack&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;
Copy after login

6.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).4 删除

delete from users
where id=9
Copy after login
6.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) Node.js使用

6.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) 初始化

  • 导包:npm i mysql
//引入mysql
const mysql = require(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;mysql&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
//建立数据库连接
const db = mysql.createPool({
    url: &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)7.0.0.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;,//数据库IP地址
    user: &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;root&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;,//账号
    password: &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)456&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;,//密码
    database: &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;test_db&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;//操作哪一个数据库
});
Copy after login

6.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) 查询

const queryStr = &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;select * from users&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;;
db.query(queryStr, (err, results) => {
    if (err) return console.log(err.message);
    console.log(results);
})

PS E:\FED\js\node.js\node.js—资料\day总复习\code> node .\This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)8-mysql操作.js
[
  RowDataPacket { id: This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text), username: &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;zz&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, password: &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, status: 0 },
  RowDataPacket { id: This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text), username: &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;ls&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, password: &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;abc&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, status: 0 },
  RowDataPacket { id: 4, username: &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;jony&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, password: &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;456&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, status: 0 }
]
Copy after login

6.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) 插入

const user = { username: &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;superman&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, password: &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;jknad&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9; };
const insertStr = &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;insert into users set ?&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;;
db.query(insertStr, user, (err, results) => {
    if (err) return console.log(err.message);
    if (results.affectedRows == This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)) {
        console.log(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;插入数据成功&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
    }
})
//插入数据成功
Copy after login

6.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).4 更新

const user = { id: This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)0, username: &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;super&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;, password: &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)456&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9; };
const updateStr = &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;update users set ? where id=?&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;;
db.query(updateStr, [user, user.id], (err, results) => {
    if (err) return console.log(err.message);
    if (results.affectedRows == This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)) {
        console.log(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;更新数据成功&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
    }
})
Copy after login

6.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).5 删除

(This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)) 一般删除
const deleteStr = &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;delete from users where id=?&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;;
db.query(deleteStr, This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)0, (err, results) => {
    if (err) return console.log(err.message);
    if (results.affectedRows == This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)) {
        console.log(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;删除成功&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
    }
})
Copy after login
(This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)) 标记删除
const deleteStr = &#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;update users set status=This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) where id=?&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;;
db.query(deleteStr, This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)0, (err, results) => {
    if (err) return console.log(err.message);
    if (results.affectedRows == This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)) {
        console.log(&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;删除成功&#This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9;);
    }
})
Copy after login
七、前后端的身份认证article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)>7.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) Web开发模式article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)>

7.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) 基于服务端渲染的传统 Web 开发模式

概念:服务端在后台拼接html页面,发送给客户端,不需要ajax

特点

  • 前端耗时少
  • 有利于SEO
  • 占用服务端资源
  • 不利于前后端分离开发

7.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) 基于前后端分离的新型 Web 开发模式

概念:后端提供API接口,前端通过ajax调用接口

特点

  • 开发体验好
  • 用户体验好
  • 减轻服务器渲染压力
  • 不利于SEO

不谈业务场景而盲目选择使用何种开发模式都是耍流氓

  • For example, enterprise-level website, the main function is display without complex interaction, and requires good SEO, then we need to use server Side rendering
  • And similar to Backend management project, interactivity is relatively strong, there is no need to consider SEO, then you can use Front-end and back-end separation Development model
  • In addition, the specific development model to be used is not absolute. In order to take into account both the rendering speed of the homepage and the development efficiency of front-end and back-end separation, some websites adopt First screen server-side rendering of other pagesDevelopment model with front-end and back-end separation
##7.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) Identity Authenticationarticle will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)>

Concept: Authenticate the customer’s identity through different means (Verification code, password, face, fingerprint...)

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)

7.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) Session authentication mechanismarticle will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)>

7.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) Cookie

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)

Cookie: Stored in the browser as a string not exceeding 4KB, stored in the form of key-value pair

    Automatically sent
  • Independent domain name
  • Expiration time limit
  • 4KB limit
  • Easy to forge, not recommended to store
  • private data

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)

7.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) Session

Core:Member Card POS Machine Authentication

This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)

  • npm install express-session
For more node-related knowledge, please visit:

nodejs tutorial!

The above is the detailed content of This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!