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!
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/
① 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...
Download link: https://nodejs.org/en/
:node –v
Learning route: JavaScript basic syntax Node.js built-in API modules (fs, path, http, etc.) Third-party API modules (express, mysql, etc.)
: node js file name
: ①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
##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##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
// 引用内部模块 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;); })
readFilecan only read existing files The file
already has a file to write, then create a file with the same name, and then write the file
needs to be written in
writeFile
(This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)) Prevent dynamic splicingcommand automatically splices the current path and the js script file path
regardless of
We can use Absolute path
Improvement
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 moduleconst 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
定义:Node.js
提供创建web服务器
// 导入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;); })
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;); })
定义:用户自定义的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;);
注意:自定义模块开头必须有./ …/
定义:由第三方
提供,使用前需要下载
//下载外部导入 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;);
npm i nodemon -g
代替node
使用nodedmon
,每次修改内容不需要重启服务器,自动监听
模块作用域定义
:和函数一致,当前模块定义的方法、变量,只能在当前模块
访问,防止变量污染
暴露
:通过module.exports
或者exports
暴露出去,使用 require()
方法导入模块时,导入的结果,永远以module.exports
指向的对象为准
定义: 一次加载缓存,从缓存加载
,内置模块
加载优先级MAX
包:概念像node.js
的第三方模块
,包是基于内置模块
封装出来的,提供了更高级、更方便的 API
,极大的提高了开发效率
npm: 包管理工具
安装包
导入包
使用包
// 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
包的版本号是以“点分十进制”形式进行定义的,总共有三位数字,例如 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)
node_modules
文件夹用来存放所有已安装到项目中的包。require() 导入第三方包时,就是从这个目录中查找并加载包。package-lock.json
配置文件用来记录 node_modules 目录下的每一个包的下载信息,例如包的名字、版本号、下载地址等。package.json
项目的名称、版本号、描述等、用到了哪些包、开发期间使用的包、部署使用的包devDependencies
:开发依赖dependencies
:核心依赖//安装包 npm i moment //安装全局包 npm i 包名 -g //安装包到开发阶段到devDependencies npm i 包名 -D //安装所有依赖包 npm install //卸载包 npm uninstall moment //查看已经安装的局部包 npm ls //查看全局安装的包 npm ls -g
查看包命令: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
//查看当前npm镜像 npm config get registry //nrm镜像工具,安装为全局镜像 nrm ls //切换镜像 nrm use taobao
一个规范的包,它的组成结构,必须符合以下 This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) 点要求:
单独的目录
而存在package.json
这个包管理配置文件name,version,main
这三个属性,分别代表包的名字、版本号、包的入口
发布包到npm
上
npm login
登录npm publish
npm unpublish 包名 --force
资源:
Express
:基于Node.js http
进一步封装,更加高级的Web开发框架
对于前端程序员来说,最常见的两种服务器,分别是:
Web 网页资源
的服务器 API 接口
的服务器//导入包 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;); })
定义
:通过路径暴露文件,省去文件路径的描写
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;); })
推荐VScode插件:postcode
Express 的中文官网: http://www.expressjs.com.cn/
定义
:客户端与服务器映射关系
//导入包 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;); })
为了方便对路由进行模块化的管理
,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;
使用路由对象
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;); })
中间件:与路由处理函数不同,必须包含next参数
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;); })
注意
多个中间件共享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;); })
路由之前
调用中间件
next()函数
不能忘,后面不用写内容
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;); })
定义:绑定到 express.Router()
实例上的中间件
定义:捕获项目
错误,防止出错,在所有路由之后
定义
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!服务器出错
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; }
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;); })
注意:Express 内置的 express.urlencoded
中间件,就是基于 body-parser
这个第三方中间件进一步封装出来的。
封装中间件
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;
测试中间件
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;); })
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;); })
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;
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 }); })
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
概念:由Http
响应头构成,决定浏览器
是否阻止js代码
获取资源,在服务器端
配置
//只允许特定的域名访问、*代表全部 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;);
无自定义头部字段
、Accept、Accept-Language、Content-Language、DPR、Downlink、Save-Data、Viewport-Width、Width 、Content-Type(只有三个值application/x-www-form-urlencoded、multipart/form-data、text/plain)一次请求
自定义
头部字段OPTION预检
,成功后发送带有数据的请求概念:只支持GET
请求
定义:组织
、存储
、管理数据
的仓库
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
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;)
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;
delete from users where id=9
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;//操作哪一个数据库 });
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 } ]
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;); } }) //插入数据成功
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;); } })
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;); } })
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;); } })
概念:服务端在后台拼接html页面
,发送给客户端,不需要ajax
特点:
概念:后端提供API
接口,前端通过ajax
调用接口
特点:
不谈业务场景而盲目选择使用何种开发模式都是耍流氓
enterprise-level website
, the main function is display without complex interaction, and requires good SEO
, then we need to use server Side rendering
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 modelFirst screen server-side rendering of other pages
Development model with front-end and back-end separationConcept: Authenticate the customer’s identity through different means (Verification code, password, face, fingerprint...)
Cookie: Stored in the browser as a string not exceeding 4KB, stored in the form of
key-value pair
Core:Member Card POS Machine Authentication
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!