-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1742893792.html
More file actions
919 lines (584 loc) · 43.7 KB
/
Copy path1742893792.html
File metadata and controls
919 lines (584 loc) · 43.7 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
<!DOCTYPE html>
<html lang="en" data-default-color-scheme="auto">
<head>
<meta charset="UTF-8">
<link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
<link rel="icon" href="/img/fluid.png">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="theme-color" content="#2f4154">
<meta name="author" content="FangZhou">
<meta name="keywords" content="express,nodejs,frontend">
<meta name="description" content="Express轻量化Node.js Web开发框架学习笔记,涵盖路由、中间件和模板引擎">
<meta property="og:type" content="article">
<meta property="og:title" content="Express:轻量化的Web开发框架">
<meta property="og:url" content="http://arkpln.github.io/1742893792.html">
<meta property="og:site_name" content="Ark的技术博客">
<meta property="og:description" content="Express轻量化Node.js Web开发框架学习笔记,涵盖路由、中间件和模板引擎">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2024-10-27T14:37:23.000Z">
<meta property="article:modified_time" content="2026-02-28T13:38:16.934Z">
<meta property="article:author" content="FangZhou">
<meta property="article:tag" content="express">
<meta property="article:tag" content="nodejs">
<meta property="article:tag" content="web-framework">
<meta property="article:tag" content="backend">
<meta name="twitter:card" content="summary_large_image">
<title>Express:轻量化的Web开发框架 - Ark的技术博客</title>
<link rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css">
<link rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css">
<link rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css">
<!-- 主题依赖的图标库,不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->
<link rel="stylesheet" href="//at.alicdn.com/t/c/font_1749284_5i9bdhy70f8.css">
<link rel="stylesheet" href="//at.alicdn.com/t/c/font_1736178_k526ubmyhba.css">
<link rel="stylesheet" href="/css/main.css">
<link id="highlight-css" rel="stylesheet" href="/css/highlight.css">
<link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css">
<script id="fluid-configs">
var Fluid = window.Fluid || {};
Fluid.ctx = Object.assign({}, Fluid.ctx)
var CONFIG = {"hostname":"arkpln.github.io","root":"/","version":"1.9.8","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":true,"follow_dnt":true,"baidu":null,"google":{"measurement_id":null},"tencent":{"sid":null,"cid":null},"leancloud":{"app_id":"mEwkD7geCX7RWNO8srhDE2oP-gzGzoHsz","app_key":"4EF76hyiVA1QILHZThVbfoat","server_url":"https://mewkd7ge.lc-cn-n1-shared.com","path":"window.location.pathname","ignore_local":false},"umami":{"src":null,"website_id":null,"domains":null,"start_time":"2024-01-01T00:00:00.000Z","token":null,"api_server":null}},"search_path":"/local-search.xml","include_content_in_search":true};
if (CONFIG.web_analytics.follow_dnt) {
var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
}
</script>
<script src="/js/utils.js"></script>
<script src="/js/color-schema.js"></script>
<meta name="google-site-verification" content="5KfTZvtlydmBIzY97rVo-caP5jljAfmD3Qs3sBzY2Go"><style>ins.adsbygoogle[data-ad-status="unfilled"] { display: none !important; }</style>
<meta name="generator" content="Hexo 7.3.0"></head>
<body>
<header>
<div class="header-inner" style="height: 70vh;">
<nav id="navbar" class="navbar fixed-top navbar-expand-lg navbar-dark scrolling-navbar">
<div class="container">
<a class="navbar-brand" href="/">
<strong>Ark的技术博客</strong>
</a>
<button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<div class="animated-icon"><span></span><span></span><span></span></div>
</button>
<!-- Collapsible content -->
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ml-auto text-center">
<li class="nav-item">
<a class="nav-link" href="/" target="_self">
<i class="iconfont icon-home-fill"></i>
<span>快乐老家</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/archives/" target="_self">
<i class="iconfont icon-archive-fill"></i>
<span>笔岚档案</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/categories/" target="_self">
<i class="iconfont icon-category-fill"></i>
<span>文以类聚</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/tags/" target="_self">
<i class="iconfont icon-tags-fill"></i>
<span>按标签阅览</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/about/" target="_self">
<i class="iconfont icon-user-fill"></i>
<span>联系我</span>
</a>
</li>
<li class="nav-item" id="search-btn">
<a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
<i class="iconfont icon-search"></i>
</a>
</li>
<li class="nav-item" id="color-toggle-btn">
<a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
<i class="iconfont icon-dark" id="color-toggle-icon"></i>
</a>
</li>
</ul>
</div>
</div>
</nav>
<div id="banner" class="banner" parallax="true" style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
<div class="full-bg-img">
<div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
<div class="banner-text text-center fade-in-up">
<div class="h2">
<span id="subtitle" data-typed-text="Express:轻量化的Web开发框架"></span>
</div>
<div class="mt-3">
<span class="post-meta">
<i class="iconfont icon-date-fill" aria-hidden="true"></i>
<time datetime="2024-10-27 22:37" pubdate>
October 27, 2024 pm
</time>
</span>
</div>
<div class="mt-1">
<span class="post-meta mr-2">
<i class="iconfont icon-chart"></i>
4.8k words
</span>
<span class="post-meta mr-2">
<i class="iconfont icon-clock-fill"></i>
41 mins
</span>
</div>
</div>
</div>
</div>
</div>
</div>
</header>
<main>
<div class="container-fluid nopadding-x">
<div class="row nomargin-x">
<div class="side-col d-none d-lg-block col-lg-2">
<aside class="sidebar d-none d-xl-block" style="margin-right:-1rem;z-index:-1"><ins class="adsbygoogle" style="display:flex;justify-content:center;min-width:160px;max-width:300px;width:100%;height:600px;position:sticky;top:2rem" data-ad-client="ca-pub-3240358922862141" data-ad-slot="6522434837"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script></aside>
</div>
<div class="col-lg-8 nopadding-x-md">
<div class="container nopadding-x-md" id="board-ctn">
<div id="board">
<article class="post-content mx-auto">
<h1 id="seo-header">Express:轻量化的Web开发框架</h1>
<div class="markdown-body">
<h1 id="Express:轻量化的Web开发框架学习笔记"><a href="#Express:轻量化的Web开发框架学习笔记" class="headerlink" title="Express:轻量化的Web开发框架学习笔记"></a>Express:轻量化的Web开发框架学习笔记</h1><h2 id="Vol1-安装和使用Express"><a href="#Vol1-安装和使用Express" class="headerlink" title="Vol1.安装和使用Express"></a>Vol1.安装和使用Express</h2><h3 id="1-1-安装Express"><a href="#1-1-安装Express" class="headerlink" title="1.1 安装Express"></a>1.1 安装Express</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">$ </span><span class="language-bash">npm install -g express-generator</span><br>//在nodejs下安装Express<br>express ${targetProjectName}<br>//创建Express项目<br>npm install<br>//在项目文件夹内安装依赖<br>npm start<br>//启动项目,于http://localhost:3000<br></code></pre></td></tr></table></figure>
<h3 id="1-2-项目结构"><a href="#1-2-项目结构" class="headerlink" title="1.2 项目结构"></a>1.2 项目结构</h3><h4 id="Express默认应用中文件及其作用"><a href="#Express默认应用中文件及其作用" class="headerlink" title="Express默认应用中文件及其作用"></a>Express默认应用中文件及其作用</h4><table>
<thead>
<tr>
<th>目录名/文件名</th>
<th>类型</th>
<th>作用</th>
</tr>
</thead>
<tbody><tr>
<td>bin</td>
<td>目录</td>
<td>服务器脚本默认目录</td>
</tr>
<tr>
<td>bin/<a target="_blank" rel="external nofollow noopener noreferrer" href="http://www.js/">www.js</a></td>
<td>文件</td>
<td>服务器默认脚本,即启动服务脚本</td>
</tr>
<tr>
<td>node_modules</td>
<td>目录</td>
<td>依赖包安装目录,用于存放依赖包</td>
</tr>
<tr>
<td>public</td>
<td>目录</td>
<td>静态资源目录,用于存放静态资源</td>
</tr>
<tr>
<td>routes</td>
<td>目录</td>
<td>路由目录,用于存放路由文件</td>
</tr>
<tr>
<td>routes/index.js</td>
<td>文件</td>
<td>首页路由文件</td>
</tr>
<tr>
<td>routes/users.js</td>
<td>文件</td>
<td>用户路由文件</td>
</tr>
<tr>
<td>views</td>
<td>目录</td>
<td>页面目录,用于存放页面文件</td>
</tr>
<tr>
<td>views/error.jade</td>
<td>文件</td>
<td>错误页面</td>
</tr>
<tr>
<td>views/index.jade</td>
<td>文件</td>
<td>首页</td>
</tr>
<tr>
<td>views/layout.jade</td>
<td>文件</td>
<td>页面共用布局</td>
</tr>
<tr>
<td>app.js</td>
<td>文件</td>
<td>应用主文件</td>
</tr>
<tr>
<td>package.json</td>
<td>文件</td>
<td>项目配置文件</td>
</tr>
<tr>
<td>package-lock.json</td>
<td>文件</td>
<td>锁定的项目配置文件</td>
</tr>
</tbody></table>
<h3 id="1-3-Express路由"><a href="#1-3-Express路由" class="headerlink" title="1.3 Express路由"></a>1.3 Express路由</h3><h4 id="1-3-1-GET请求"><a href="#1-3-1-GET请求" class="headerlink" title="1.3.1 GET请求"></a>1.3.1 GET请求</h4><p>route/index.js路由文件,即首页路由文件,是一个GET请求路由,格式如下:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">var</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">'express'</span>) <span class="hljs-comment">//引入</span><br><span class="hljs-keyword">var</span> router = express.<span class="hljs-title class_">Router</span>() <span class="hljs-comment">//引入Express路由对象</span><br><br><span class="hljs-comment">//首页路由</span><br><br>router.<span class="hljs-title function_">get</span>(<span class="hljs-string">'/'</span>,<span class="hljs-keyword">function</span>(<span class="hljs-params">req,res,next</span>){<br> res.<span class="hljs-title function_">render</span>(<span class="hljs-string">'index'</span>,{<span class="hljs-attr">title</span>:<span class="hljs-string">'Express'</span>});<br>});<br>moudule.<span class="hljs-property">exports</span> = router<br><span class="hljs-comment">//导出路由</span><br></code></pre></td></tr></table></figure>
<h4 id="1-3-2-自定义路由"><a href="#1-3-2-自定义路由" class="headerlink" title="1.3.2 自定义路由"></a>1.3.2 自定义路由</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">//定义一个GET请求‘/world’的路由,执行方法</span><br>router.<span class="hljs-title function_">get</span>(<span class="hljs-string">'/world'</span>,<span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) {<br> res.<span class="hljs-title function_">render</span>(<span class="hljs-string">'index'</span>,{<span class="hljs-attr">title</span>: <span class="hljs-string">'Hello World!'</span>});<br>})<br></code></pre></td></tr></table></figure>
<h4 id="1-3-3-其他请求方式的路由"><a href="#1-3-3-其他请求方式的路由" class="headerlink" title="1.3.3 其他请求方式的路由"></a>1.3.3 其他请求方式的路由</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">//router.post()</span><br><span class="hljs-comment">//router.put()</span><br><span class="hljs-comment">//router.delete()</span><br></code></pre></td></tr></table></figure>
<h4 id="1-3-4-路由匹配规则"><a href="#1-3-4-路由匹配规则" class="headerlink" title="1.3.4 路由匹配规则"></a>1.3.4 路由匹配规则</h4><p><strong>模糊匹配符</strong></p>
<table>
<thead>
<tr>
<th>符号</th>
<th>含义</th>
<th>效果</th>
</tr>
</thead>
<tbody><tr>
<td>+</td>
<td>紧接着前一个字母重复</td>
<td>we+st ->weest weeest</td>
</tr>
<tr>
<td>(element)?</td>
<td>括号内元素缺失</td>
<td>w(es)?t ->wt</td>
</tr>
<tr>
<td>*</td>
<td>两端之间插入任意元素</td>
<td>we * st ->we123st</td>
</tr>
</tbody></table>
<p>同时支持了正则表达式,<a target="_blank" rel="external nofollow noopener noreferrer" href="https://www.runoob.com/regexp/regexp-tutorial.html">正则表达式 – 教程 | 菜鸟教程</a></p>
<h4 id="1-3-5-中间件"><a href="#1-3-5-中间件" class="headerlink" title="1.3.5 中间件"></a>1.3.5 中间件</h4><p>在项目开发过程中,有时需要使用共同的方法来完成请求,为了提高效率,可以将这些共同的方法单独提出来,即中间件。</p>
<p>当前端使用GET方法请求根路径的时候,会进入第二个参数function中,这个路由处理方法就是Express中的中间件。</p>
<p>在Express中,中间件会被传入三个形参。</p>
<table>
<thead>
<tr>
<th>参数顺序</th>
<th>形参名</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td>req</td>
<td>请求数据对象Request</td>
</tr>
<tr>
<td>2</td>
<td>res</td>
<td>返回数据对象Response</td>
</tr>
<tr>
<td>3</td>
<td>next</td>
<td>下一步函数</td>
</tr>
</tbody></table>
<p>例如,实现验证功能,可以在路由处理方法前再加一个方法</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs js">router.<span class="hljs-title function_">get</span>(<span class="hljs-string">'/car'</span>,<span class="hljs-keyword">function</span>(<span class="hljs-params">req,res,next</span>){<br> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'这里是中间件'</span>);<br> <span class="hljs-title function_">next</span>();<br>},<span class="hljs-keyword">function</span>(<span class="hljs-params">req,res,next</span>){<br> res.<span class="hljs-title function_">send</span>(<span class="hljs-string">'这里是返回'</span>);<br>});<br></code></pre></td></tr></table></figure>
<h3 id="1-4-Express页面"><a href="#1-4-Express页面" class="headerlink" title="1.4 Express页面"></a>1.4 Express页面</h3><h4 id="1-4-1-更换模板引擎"><a href="#1-4-1-更换模板引擎" class="headerlink" title="1.4.1 更换模板引擎"></a>1.4.1 更换模板引擎</h4><p>Express的默认引擎是jade,为了便于开发,可以换成更简洁的art-template。</p>
<p>安装依赖</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs shell">npm install -S art-template<br>npm install -S express-art-template<br></code></pre></td></tr></table></figure>
<p>修改项目根目录下的<code>app.js</code>,切换模板引擎。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs js">app.<span class="hljs-title function_">set</span>(<span class="hljs-string">'view engine'</span>,<span class="hljs-string">'jade'</span>);<br><span class="hljs-comment">//原</span><br>app.<span class="hljs-title function_">engine</span>(<span class="hljs-string">'.html'</span>,<span class="hljs-built_in">require</span>(<span class="hljs-string">'express-art-template'</span>));<br>app.<span class="hljs-title function_">set</span>(<span class="hljs-string">'view engine'</span>,<span class="hljs-string">'html'</span>)<br></code></pre></td></tr></table></figure>
<h4 id="1-4-2-渲染数据到页面上"><a href="#1-4-2-渲染数据到页面上" class="headerlink" title="1.4.2 渲染数据到页面上"></a>1.4.2 渲染数据到页面上</h4><p>在Express中将数据渲染到页面上的方法是Response对象的render方法,例如:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs js">router.<span class="hljs-title function_">get</span>(<span class="hljs-string">'/'</span>, <span class="hljs-keyword">function</span>(<span class="hljs-params">req, res, next</span>) {<br> res.<span class="hljs-title function_">render</span>(<span class="hljs-string">'index'</span>, { <span class="hljs-attr">title</span>: <span class="hljs-string">'hello'</span>});<br>});<br></code></pre></td></tr></table></figure>
<p>这段代码向index.html传入了一个键值对{ title: ‘hello’},在html界面可以使用双花括号来接收字段:</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag"><<span class="hljs-name">p</span>></span>This is the value of title variable: {{ title }}<span class="hljs-tag"></<span class="hljs-name">p</span>></span><br></code></pre></td></tr></table></figure>
<h4 id="1-4-3-条件渲染"><a href="#1-4-3-条件渲染" class="headerlink" title="1.4.3 条件渲染"></a>1.4.3 条件渲染</h4><h5 id="1-基本条件渲染"><a href="#1-基本条件渲染" class="headerlink" title="1.基本条件渲染"></a>1.基本条件渲染</h5><p><em>Vue.js:在想我的事情?</em></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag"><<span class="hljs-name">p</span>></span>This is the value of title variable: {{ title }}<span class="hljs-tag"></<span class="hljs-name">p</span>></span><br>{{if age < 30}}<br><span class="hljs-tag"><<span class="hljs-name">p</span>></span>今年{{ age }}岁了,还没到30岁生日,加油!<span class="hljs-tag"></<span class="hljs-name">p</span>></span><br>{{/if}}<br>{{if age > 30}}<br><span class="hljs-tag"><<span class="hljs-name">p</span>></span>今年{{ age }}岁了,30岁生日已经过了<span class="hljs-tag"></<span class="hljs-name">p</span>></span><br>{{/if}}<br><br><br>//格式为{{if CONDITION}} <span class="hljs-tag"></></span> {{/if}}<br></code></pre></td></tr></table></figure>
<h5 id="2-嵌套条件渲染"><a href="#2-嵌套条件渲染" class="headerlink" title="2.嵌套条件渲染"></a>2.嵌套条件渲染</h5><p>同理,略。</p>
<h4 id="1-4-4-渲染循环"><a href="#1-4-4-渲染循环" class="headerlink" title="1.4.4 渲染循环"></a>1.4.4 渲染循环</h4><h5 id="基本渲染循环"><a href="#基本渲染循环" class="headerlink" title="基本渲染循环"></a>基本渲染循环</h5><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-comment"><!-- index.html --></span><br><span class="hljs-tag"><<span class="hljs-name">h2</span>></span>循环渲染<span class="hljs-tag"></<span class="hljs-name">h2</span>></span><br>{{each list as item}}<br><span class="hljs-tag"><<span class="hljs-name">p</span>></span>数据id{{item.id}},内容{{item.content}}<span class="hljs-tag"></<span class="hljs-name">p</span>></span><br>{{/each}}<br></code></pre></td></tr></table></figure>
<p>在art-template中,循环渲染的固定写法是<code>{{each LIST as ITEM}}</code></p>
<h3 id="1-5-请求对象Request"><a href="#1-5-请求对象Request" class="headerlink" title="1.5 请求对象Request"></a>1.5 请求对象Request</h3><h4 id="1-5-1-Request-url属性"><a href="#1-5-1-Request-url属性" class="headerlink" title="1.5.1 Request.url属性"></a>1.5.1 Request.url属性</h4><p>Request.url属性可以获得请求地址。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs js">#./routes/index.<span class="hljs-property">js</span><br>router.<span class="hljs-title function_">get</span>(<span class="hljs-string">'/abcd'</span>,<span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) {<br> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(req.<span class="hljs-property">url</span>);<br> res.<span class="hljs-title function_">render</span>(<span class="hljs-string">'index'</span>,{<span class="hljs-attr">title</span>:<span class="hljs-string">'Express'</span>})<br>})<br><br></code></pre></td></tr></table></figure>
<h4 id="1-5-2-Request-query属性"><a href="#1-5-2-Request-query属性" class="headerlink" title="1.5.2 Request.query属性"></a>1.5.2 Request.query属性</h4><p>Request.query常用来获取GET请求参数,它是一个对象,包含路由中每个查询字符串参数的属性。<strong>只能获得GET请求方式</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs js">router.<span class="hljs-title function_">get</span>(<span class="hljs-string">'/book'</span>,<span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) {<br> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(req.<span class="hljs-property">query</span>);<br> res.<span class="hljs-title function_">render</span>(<span class="hljs-string">'index'</span>,{<span class="hljs-attr">title</span>:<span class="hljs-string">'Express'</span>});<br>})<br></code></pre></td></tr></table></figure>
<h4 id="1-5-3-Request-body属性"><a href="#1-5-3-Request-body属性" class="headerlink" title="1.5.3 Request.body属性"></a>1.5.3 Request.body属性</h4><p>Express将POST请求参数封装在了Request.body对象中,以键值对形式存在。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs js">router.<span class="hljs-title function_">get</span>(<span class="hljs-string">'/abc'</span>,<span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) {<br> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(req.<span class="hljs-property">body</span>);<br> res.<span class="hljs-title function_">render</span>(<span class="hljs-string">'index'</span>,{<span class="hljs-attr">title</span>:<span class="hljs-string">'Express'</span>});<br>})<br></code></pre></td></tr></table></figure>
<h4 id="1-5-4-Request-params属性"><a href="#1-5-4-Request-params属性" class="headerlink" title="1.5.4 Request.params属性"></a>1.5.4 Request.params属性</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs js">router.<span class="hljs-title function_">get</span>(<span class="hljs-string">'/book/:id'</span>,<span class="hljs-keyword">function</span>(<span class="hljs-params">req,res,next</span>){<br> res.<span class="hljs-title function_">send</span>(req.<span class="hljs-property">params</span>)<br> <span class="hljs-comment">// 此时返回{id:2}</span><br> res.<span class="hljs-title function_">render</span>(<span class="hljs-string">'index'</span>,{<span class="hljs-attr">title</span>:<span class="hljs-string">'Express'</span>});<br>})<br></code></pre></td></tr></table></figure>
<p>Express通过URL传入的参数存到了Request.params属性,同时也是一个对象,可以获取自定义URL的任意一个参数。</p>
<h4 id="1-5-5-Request-headers属性"><a href="#1-5-5-Request-headers属性" class="headerlink" title="1.5.5 Request.headers属性"></a>1.5.5 Request.headers属性</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs js">router.<span class="hljs-title function_">post</span>(<span class="hljs-string">'/abc'</span>,<span class="hljs-keyword">function</span>(<span class="hljs-params">req,res,next</span>){<br> res.<span class="hljs-title function_">send</span>(req.<span class="hljs-property">headers</span>);<br>})<br></code></pre></td></tr></table></figure>
<p>Express也可以获取到请求头的数据,存在Request.header中。</p>
<h4 id="1-5-6-Request-cookies属性"><a href="#1-5-6-Request-cookies属性" class="headerlink" title="1.5.6 Request.cookies属性"></a>1.5.6 Request.cookies属性</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs js">router.<span class="hljs-title function_">post</span>(<span class="hljs-string">'/abc'</span>,<span class="hljs-keyword">function</span>(<span class="hljs-params">req,res,next</span>){<br> res.<span class="hljs-title function_">send</span>(req.<span class="hljs-property">cookies</span>);<br>})<br></code></pre></td></tr></table></figure>
<p>Express提供了简单的获取客户端cookies的方式,并以对象方式储存。</p>
<h3 id="1-6-返回对象Response"><a href="#1-6-返回对象Response" class="headerlink" title="1.6 返回对象Response"></a>1.6 返回对象Response</h3><h4 id="1-6-1-Response-render-方法"><a href="#1-6-1-Response-render-方法" class="headerlink" title="1.6.1 Response.render()方法"></a>1.6.1 Response.render()方法</h4><p><code>Response.render(#view:String,locals:Object,callback:Function)</code></p>
<p><strong>#view:</strong> 必选项,页面文件,用于渲染的文件路径。</p>
<p><strong>locals:</strong> 属性定义页面的局部变量。</p>
<p><strong>callback:</strong> 回调函数,返回困难的错误和呈现的字符串,但不执行自动响应。发生错误时在<code>next(err)</code>中内部调用。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs js">res.<span class="hljs-title function_">render</span>(<span class="hljs-string">'index'</span>) <span class="hljs-comment">//渲染一个页面文件到客户端</span><br><br><br>res.<span class="hljs-title function_">render</span>(<span class="hljs-string">'index'</span>,<span class="hljs-keyword">function</span>(<span class="hljs-params">err,html</span>){<br> res.<span class="hljs-title function_">send</span>(html);<br> <span class="hljs-comment">//回调函数,明确指定发送的HTML字符串</span><br>})<br><br>res.<span class="hljs-title function_">render</span>(<span class="hljs-string">'user'</span>,{<span class="hljs-attr">name</span>:<span class="hljs-string">'Tobi'</span>},<span class="hljs-keyword">function</span>(<span class="hljs-params">err,html</span>))<br><span class="hljs-comment">//渲染完毕的回调函数</span><br><br></code></pre></td></tr></table></figure>
</div>
<hr>
<div>
<div class="post-metas my-3">
<div class="post-meta mr-3 d-flex align-items-center">
<i class="iconfont icon-category"></i>
<span class="category-chains">
<span class="category-chain">
<a href="/categories/backend/" class="category-chain-item">backend</a>
</span>
</span>
</div>
<div class="post-meta">
<i class="iconfont icon-tags"></i>
<a href="/tags/express/" class="print-no-link">#express</a>
<a href="/tags/nodejs/" class="print-no-link">#nodejs</a>
<a href="/tags/web-framework/" class="print-no-link">#web-framework</a>
<a href="/tags/backend/" class="print-no-link">#backend</a>
</div>
</div>
<div class="license-box my-3">
<div class="license-title">
<div>Express:轻量化的Web开发框架</div>
<div>http://arkpln.github.io/1742893792.html</div>
</div>
<div class="license-meta">
<div class="license-meta-item">
<div>Author</div>
<div>FangZhou</div>
</div>
<div class="license-meta-item license-meta-date">
<div>Posted on</div>
<div>October 27, 2024</div>
</div>
<div class="license-meta-item">
<div>Licensed under</div>
<div>
<a class="print-no-link" target="_blank" href="https://creativecommons.org/licenses/by/4.0/" rel="external nofollow noopener noreferrer">
<span class="hint--top hint--rounded" aria-label="BY - Attribution">
<i class="iconfont icon-cc-by"></i>
</span>
</a>
</div>
</div>
</div>
<div class="license-icon iconfont"></div>
</div>
<div style="width:100%;display:flex;justify-content:center;margin-bottom:1.5rem"><ins class="adsbygoogle" style="display:flex;justify-content:center;max-width:845px;width:100%;height:90px" data-ad-client="ca-pub-3240358922862141" data-ad-slot="6522434837"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script></div>
<div class="post-prevnext my-3">
<article class="post-prev col-6">
<a href="/303649792.html" title="React学习笔记">
<i class="iconfont icon-arrowleft"></i>
<span class="hidden-mobile">React学习笔记</span>
<span class="visible-mobile">Previous</span>
</a>
</article>
<article class="post-next col-6">
<a href="/1901149387.html" title="Vue学习笔记">
<span class="hidden-mobile">Vue学习笔记</span>
<span class="visible-mobile">Next</span>
<i class="iconfont icon-arrowright"></i>
</a>
</article>
</div>
</div>
<article id="comments" lazyload>
<div id="valine"></div>
<script type="text/javascript">
Fluid.utils.loadComments('#valine', function() {
Fluid.utils.createScript('https://lib.baomitu.com/valine/1.5.1/Valine.min.js', function() {
var options = Object.assign(
{"appId":"mEwkD7geCX7RWNO8srhDE2oP-gzGzoHsz","appKey":"4EF76hyiVA1QILHZThVbfoat","path":"window.location.pathname","placeholder":null,"avatar":"retro","meta":["nick","mail","link"],"requiredFields":[],"pageSize":10,"lang":"zh-CN","highlight":false,"recordIP":false,"serverURLs":"","emojiCDN":null,"emojiMaps":null,"enableQQ":false,"el":"#vcomments"},
{
el: "#valine",
path: window.location.pathname
}
)
new Valine(options);
Fluid.utils.waitElementVisible('#valine .vcontent', () => {
var imgSelector = '#valine .vcontent img:not(.vemoji)';
Fluid.plugins.imageCaption(imgSelector);
Fluid.plugins.fancyBox(imgSelector);
})
});
});
</script>
<noscript>Please enable JavaScript to view the comments</noscript>
</article>
</article>
</div>
</div>
</div>
<div class="side-col d-none d-lg-block col-lg-2">
<aside class="sidebar" style="margin-left: -1rem">
<div id="toc">
<p class="toc-header">
<i class="iconfont icon-list"></i>
<span>Table of Contents</span>
</p>
<div class="toc-body" id="toc-body"></div>
</div>
</aside>
</div>
</div>
</div>
<a id="scroll-top-button" aria-label="TOP" href="#" role="button">
<i class="iconfont icon-arrowup" aria-hidden="true"></i>
</a>
<div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
<div class="modal-content">
<div class="modal-header text-center">
<h4 class="modal-title w-100 font-weight-bold">Search</h4>
<button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body mx-3">
<div class="md-form mb-5">
<input type="text" id="local-search-input" class="form-control validate">
<label data-error="x" data-success="v" for="local-search-input">Keyword</label>
</div>
<div class="list-group" id="local-search-result"></div>
</div>
</div>
</div>
</div>
</main>
<footer>
<div class="footer-inner">
<div class="footer-content">
<a href="https://hexo.io" target="_blank" rel="external nofollow noopener noreferrer"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="external nofollow noopener noreferrer"><span>Fluid</span></a>
</div>
<div class="statistics">
<span id="leancloud-site-pv-container" style="display: none">
总访问量
<span id="leancloud-site-pv"></span>
次
</span>
<span id="leancloud-site-uv-container" style="display: none">
总访客数
<span id="leancloud-site-uv"></span>
人
</span>
</div>
</div>
</footer>
<!-- Scripts -->
<script src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js"></script>
<link rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css">
<script>
NProgress.configure({"showSpinner":false,"trickleSpeed":100})
NProgress.start()
window.addEventListener('load', function() {
NProgress.done();
})
</script>
<script src="https://lib.baomitu.com/jquery/3.6.4/jquery.min.js"></script>
<script src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js"></script>
<script src="/js/events.js"></script>
<script src="/js/plugins.js"></script>
<script src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js"></script>
<script>
(function (window, document) {
var typing = Fluid.plugins.typing;
var subtitle = document.getElementById('subtitle');
if (!subtitle || !typing) {
return;
}
var text = subtitle.getAttribute('data-typed-text');
typing(text);
})(window, document);
</script>
<script src="/js/img-lazyload.js"></script>
<script>
Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.20.1/tocbot.min.js', function() {
var toc = jQuery('#toc');
if (toc.length === 0 || !window.tocbot) { return; }
var boardCtn = jQuery('#board-ctn');
var boardTop = boardCtn.offset().top;
window.tocbot.init(Object.assign({
tocSelector : '#toc-body',
contentSelector : '.markdown-body',
linkClass : 'tocbot-link',
activeLinkClass : 'tocbot-active-link',
listClass : 'tocbot-list',
isCollapsedClass: 'tocbot-is-collapsed',
collapsibleClass: 'tocbot-is-collapsible',
scrollSmooth : true,
includeTitleTags: true,
headingsOffset : -boardTop,
}, CONFIG.toc));
if (toc.find('.toc-list-item').length > 0) {
toc.css('visibility', 'visible');
}
Fluid.events.registerRefreshCallback(function() {
if ('tocbot' in window) {
tocbot.refresh();
var toc = jQuery('#toc');
if (toc.length === 0 || !tocbot) {
return;
}
if (toc.find('.toc-list-item').length > 0) {
toc.css('visibility', 'visible');
}
}
});
});
</script>
<script src="https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js"></script>
<script>Fluid.plugins.codeWidget();</script>
<script>
Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/5.0.0/anchor.min.js', function() {
window.anchors.options = {
placement: CONFIG.anchorjs.placement,
visible : CONFIG.anchorjs.visible
};
if (CONFIG.anchorjs.icon) {
window.anchors.options.icon = CONFIG.anchorjs.icon;
}
var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
var res = [];
for (var item of el) {
res.push('.markdown-body > ' + item.trim());
}
if (CONFIG.anchorjs.placement === 'left') {
window.anchors.options.class = 'anchorjs-link-left';
}
window.anchors.add(res.join(', '));
Fluid.events.registerRefreshCallback(function() {
if ('anchors' in window) {
anchors.removeAll();
var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
var res = [];
for (var item of el) {
res.push('.markdown-body > ' + item.trim());
}
if (CONFIG.anchorjs.placement === 'left') {
anchors.options.class = 'anchorjs-link-left';
}
anchors.add(res.join(', '));
}
});
});
</script>
<script>
Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
Fluid.plugins.fancyBox();
});
</script>
<script>Fluid.plugins.imageCaption();</script>
<script defer src="/js/leancloud.js"></script>
<script src="/js/local-search.js"></script>
<!-- 主题的启动项,将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script src="/js/boot.js"></script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3240358922862141" crossorigin="anonymous"></script>
<noscript>
<div class="noscript-warning">Blog works best with JavaScript enabled</div>
</noscript>
</body>
</html>