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!
4/6This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)bffcbecff5c8This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)0.jpg)
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)>
4/a0aThis%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)c88This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)eThis%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)0cThis%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)940b64c8This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)e6This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)07eb7d-This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text).png)
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/
4/04This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)bThis%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)a9This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)9405ba8d7bThis%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)9dfcThis%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)0c76cf7This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)-This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text).png)
##LTS- : Long-term stable version
Current- :Early adopter version
View version number
:node –v
4/bbec67aThis%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)4664aThis%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)afThis%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)c96This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)cThis%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)7755This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)5ab-This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text).png)
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
4/bbec67aThis%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)4664aThis%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)afThis%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)c96This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)cThis%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)7755This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)5ab-4.png)
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 modularity
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
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 modules
Definition: 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 usage1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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);
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
js script file path
regardless of
.\day always review this path
We can use Absolute path
Improvement
(This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)) Path problem
4/d6bThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)b85This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)aThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)45This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)0b5This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)6dbd6This 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)8cThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)7ffThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)-7.png)
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()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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);
})
|
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服务器
4/d6bThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)b85This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)aThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)45This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)0b5This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)6dbd6This 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)8cThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)7ffThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)-8.png)
(This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)) 初始化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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 server = http.createServer();
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:
})
|
Copy after login
(This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)) web服务器
4/5This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)aThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)065cdThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)857This 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)5This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)d4This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)047c5bba544-9.png)
- 根据浏览器访问的
url地址
不同,返回相应的绝对路径
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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:
})
|
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模块
1 2 | 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
4/5This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)aThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)065cdThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)857This 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)5This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)d4This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)047c5bba544-This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)0.png)
注意:自定义模块开头必须有./ …/
This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).4 外部模块定义:由第三方
提供,使用前需要下载
1 2 | 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
4/5fThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)777d6This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)c94cb47This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)c9eThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)b6ca96de4bb-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).png)
监听nodemon
代替node
使用nodedmon
,每次修改内容不需要重启服务器,自动监听
This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).5 模块化处理模块作用域定义
:和函数一致,当前模块定义的方法、变量,只能在当前模块
访问,防止变量污染
4/5fThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)777d6This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)c94cb47This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)c9eThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)b6ca96de4bb-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).png)
暴露
:通过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: 包管理工具
4/cThis 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)860This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)b4This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)07da465de4This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)e0c6This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)ad64This 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)This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text).png)
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) 使用流程1 2 3 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;);
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 );
|
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修复版本
版本号提升的规则:只要前面的版本号增长了,则后面的版本号归零。
1 | 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) 参数问题4/04e5c0d6This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)56This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)4dThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)cbThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)db5cee86a4This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)c6c-This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)4.png)
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命令1 2 3 4 5 6 7 8 9 10 11 12 13 14 | npm i moment
npm i 包名 -g
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 下载镜像4/04e5c0d6This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)56This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)4dThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)cbThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)db5cee86a4This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)c6c-This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)5.png)
1 2 3 4 5 6 | npm config get registry
nrm ls
nrm use taobao
|
Copy after login
4/04e5c0d6This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)56This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)4dThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)cbThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)db5cee86a4This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)c6c-This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)6.png)
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
这三个属性,分别代表包的名字、版本号、包的入口
4/cThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)66c5b5daeThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)dThis 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)47c78f8508This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)004904-This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)7.png)
发布包到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) 基本使用1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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) => {
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:
})
|
Copy after login
4.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) 托管静态资源定义
:通过路径暴露文件,省去文件路径的描写
1 2 3 4 5 6 7 8 9 10 | 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:
})
|
Copy after login
推荐VScode插件:postcode
Express 的中文官网: http://www.expressjs.com.cn/
4.4 路由4/cThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)66c5b5daeThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)dThis 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)47c78f8508This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)004904-This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)8.png)
定义
:客户端与服务器映射关系
4/faaThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)0a7c957This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)cThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)eThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)5caThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)7fdfbfThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)5c589-This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)9.png)
4.4.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) 简单挂载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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:
})
|
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 上,而是推荐将路由抽离为单独的模块
。
将路由抽离为单独模块的步骤
如下:
创建路由对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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
使用路由对象
1 2 3 4 5 6 7 8 9 10 | 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:
})
|
Copy after login
4.5 中间件4/faaThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)0a7c957This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)cThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)eThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)5caThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)7fdfbfThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)5c589-This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)0.png)
4/faaThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)0a7c957This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)cThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)eThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)5caThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)7fdfbfThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)5c589-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).png)
中间件:与路由处理函数不同,必须包含next参数
4.5.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) 基本使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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:
})
|
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))应用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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:
})
|
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))错误
定义:捕获项目
错误,防止出错,在所有路由之后
定义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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:
})
|
Copy after login
(4)Express 内置
4/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)bThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)88ef6This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)bfThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)775865This 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)0bThis 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)0This 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)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).png)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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 (express.json());
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:
})
|
Copy after login
(5)第三方
npm install body-parse
require
导入app.use()
为全局
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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:
})
|
Copy after login
注意:Express 内置的 express.urlencoded
中间件,就是基于 body-parser
这个第三方中间件进一步封装出来的。
4.6 自定义中间件封装中间件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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
测试中间件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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:
})
|
Copy after login
4.7 接口1 2 3 4 5 6 7 8 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:
})
|
Copy after login
4.7.This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text) GET接口
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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接口
1 2 3 4 5 6 7 8 9 10 | 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) CORS5.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代码
获取资源,在服务器端
配置
4/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)bThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)88ef6This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)bfThis article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)775865This 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)0bThis 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)0This 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)7-This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)4.png)
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) 响应头
4/457ecf8This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)445d8a9664f60bfc80869b74-This article will give you an in-depth understanding of Node.js (detailed explanation with pictures and text)5.png)
1 2 3 4 5 6 | 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:
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) 查询
1 | 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) 插入
1 | 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) 更新
1 | 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 删除
1 2 | 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) 初始化
1 2 3 4 5 6 7 8 9 | 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;,
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) 查询
1 2 3 4 5 6 7 8 9 10 11 12 | 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) 插入
1 2 3 4 5 6 7 8 9 | 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 更新
1 2 3 4 5 6 7 8 | 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)) 一般删除
1 2 3 4 5 6 7 | 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)) 标记删除
1 2 3 4 5 6 7 | 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 pages
Development 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...)
4/457ecf8This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)445d8a9664f60bfc80869b74-This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)6.png)
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
4/457ecf8This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)445d8a9664f60bfc80869b74-This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)7.png)
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
4/aThis%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)bThis%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)e8d0This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)40This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)878574This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)a467a8958c7This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)4-This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)8.png)
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
4/aThis%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)bThis%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)e8d0This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)40This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)878574This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)a467a8958c7This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)4-This%20article%20will%20give%20you%20an%20in-depth%20understanding%20of%20Node.js%20(detailed%20explanation%20with%20pictures%20and%20text)9.png)
- 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!