当前位置:网站首页>Express architecture in nodejs
Express architecture in nodejs
2022-04-21 08:06:00 【Fish that want to be taken away】
Express
website :https://www.expressjs.com.cn/
Express Execution principle : First find out whether there is a corresponding static resource , See if there are static resources , If there are static resources, execute static resources , If there are no static resources, find the route matching dynamic resources , Find dynamic resources , Execute dynamic resources , If not, execute 404.
middleware : Middleware mainly refers to encapsulating all Http How to request details , It's from Http Processing method from request initiation to response end . Its biggest feature , It's a middleware processing , Pass it on to the next middleware .
Global installation
-
Install the generator to the global
npm install -g express-generator -
Create a new directory , And enter the directory from the command line
-
Execute the command to generate the project skeleton
express -
Execute the command to download all dependent packages
npm install -
Execute the command to start the server
npm start
Partial installation
-
Create a new directory , And enter the directory from the command line
-
adopt npx Build project skeleton
npx express-generator -
Execute the command to download all dependent packages
npm installabbreviation :
npm i -
Execute the command to start the server
npm start
Content introduction
package.json Run in
"scripts": {
"start": "node ./bin/www", // If you want to start this, just npm startjiuxing
"hello": "node ./bin/www" // This is not start If you want to, you need to npm run hello Talent
},
If you want to change the port number , It's just bin Under the document www The following changes
var port = normalizePort(process.env.PORT || '3000');
route
For dynamic resources
Concept :
build Web The server , One of the most basic functions is to deal with HTTP request , And return the correct content according to the request . For different HTTP request , We need to establish a way to deal with . This article will briefly introduce Express Routing Middleware in .
Parameters to obtain
request.paramsrequest.query:get Submit the method to get parametersrequest.body:post Submit the method to get parametersres.send: Message return
Primary route
app.js Inside
// Primary route
// Access level 1 routing :http://localhost:3000/users
app.use('/', indexRouter);
app.use('/users', usersRouter);
Secondary route
routes Inside the folder
// Secondary route
// Access the secondary route :http://localhost:3000/users/reg
router.get('/', function(req, res, next) {
res.send('respond with a resource');
});
router.get('/reg', function(req, res) {
res.send('respond with a resource reg');
});
get and post The difference between
To obtain parameters : Use when you have a form post Submit , If there is no form, use get Submit
get:
1. Access is more direct and simple
2. There is a limit on the length of the requested road force , Different browsers have different restrictions
3. Parameters follow the path , Low security
4. Parameters are historical
How to get parameters
router.get('/reg', function(req, res) {
let {
username,pwd} = req.query;
console.log(" User name and password ",username,pwd);
res.send('get Submit successfully ');
});
post:
1. You must use a form or AJAX Initiate request
2. There is no limit on the length of paths and parameters
3. Parameters are placed in the request message body , Relatively high security
4. Parameter has no history
How to get parameters
<form action="/users/login" method="post">
<label for="username"> user name :</label>
<input type="text" name="username" id="username">
<br>
<label for="pwd"> password :</label>
<input type="text" name="pwd" id="pwd">
<br>
<input type="submit" value=" Submit ">
</form>
router.post('/login', function(req, res) {
let {
username,pwd} = req.body;
console.log(" User name and password :",username,pwd);
res.send('post Submit successfully ');
});
layered
effect
Responsibilities are clearer
Increase code flexibility and scalability
Improve maintainability
Three layer architecture

1. The presentation layer
Mainly accept requests from users , And data return , Provide access to applications for clients .
2. The business layer
Mainly responsible for the operation of data layer . That is to say, some data layer operations are combined .
3 Persistence layer
It mainly depends on whether the data layer contains logical processing , In fact, its functions mainly complete the operation of data files . Without any other operation .
MVC Three layer architecture
Only belong to the presentation layer
Pagination
1. effect
- Improve rendering
- Improve user experience
2. How to paginate
Front end paging
- ui Component sub packaged paging control
Back end paging
- Program code paging
- advantage : generality
- shortcoming : Memory consumption is too high , Performance degradation
- Database query paging ‘( The main use of )
- advantage : Low memory consumption , High performance
- shortcoming : Low versatility
3. The core concept
- Page number
- Number of displays per page
- The total number of data
- Total page number =
Math.ceil( The total number of data / Number of displays per page )
db.getCollection("students").find({
}).limit(5).skip(10);
// Page number minus 1* Display number
//limit: Number of display bars skip: Show the number of
use )
- advantage : Low memory consumption , High performance
- shortcoming : Low versatility
3. The core concept
- Page number
- Number of displays per page
- The total number of data
- Total page number =
Math.ceil( The total number of data / Number of displays per page )
db.getCollection("students").find({
}).limit(5).skip(10);
// Page number minus 1* Display number
//limit: Number of display bars skip: Show the number of
版权声明
本文为[Fish that want to be taken away]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210712347591.html
边栏推荐
猜你喜欢

Picture material free material picture material website picture material where to find some picture material download the purpose of picture material picture material product picture material website

Restful规范和使用

显示器选购参考天梯

【2022DASCTF X SU】 三月春季挑战赛 web复现

js力扣每日一题(2022/4/20)---388.文件的最长绝对路径

How can transformer break into the CV world and kill CNN?

set集合

作文以记之 ~ 每日温度

从源码角度剖析redis分布式锁

nodeJS里面的Express架构
随机推荐
类与对象的详解(构造方法的详解)
[web system course design] version 2022
【MATLAB】绘制泽尼克多项式
Virtual machine host Ping SSH campus network bridge net
Mathematical experiment -- function drawing experiment
显示器选购参考天梯
VMware 16 newly installed win11 professional edition, unable to read the ISO image, unable to start the installer
融资融券安全线是多少?
The listview column in C automatically adapts to the perfect effect of scaling
localhost和127.0.0.1有什么区别?(转载)
Php article keyword replacement class
亿级流量多级缓存架构
What is the difference between localhost and 127.0.0.1? (Reprinted)
NAS purchase reference comparison
無意中發現了一比特清華妹子的資料庫!
Replication of Apache skywalking SQL injection (cve-2020-9483)
Actual JDBC connection to MySQL database
loading加载和统一异常处理
安裝mongodb
【2022DASCTF X SU】 三月春季挑战赛 web复现