【nodejs笔记——小知识点汇总】

1.  ejs标签: <%  %> ,  <%-  %> ,  <%= %>的区别

ejs的标签分为三种:

(1)<% code %>   javascript代码

(2) <%- code  %>  显示结果会按HTML语法转换格式

(3)<%= code  %>  显示原样结果

例:

若code为<h1> hello </h1>

则 <%- code  %>   ,输出  h1大的hello;

    <%= code  %>  ,输出 <h1> hello </h1>

2.  express解析http请求 【req.query,req.body,req.params,req.param

{转自“http://blog.csdn.net/violet_day/article/details/16831577”}

get 和 post 的第一个参数都为请求的路径,第二个参数为处理请求的回调函数,回调函数有两个参数分别是 req 和 res,代表请求信息和响应信息 。路径请求及对应的获取路径有以下几种形式:

req.query

 1 // GET /search?q=tobi+ferret  
 2 req.query.q  
 3 // => "tobi ferret"  
 4 
 5 // GET /shoes?order=desc&shoe[color]=blue&shoe[type]=converse  
 6 req.query.order  
 7 // => "desc"  
 8 req.query.shoe.color  
 9 // => "blue"  
10 req.query.shoe.type  
11 // => "converse"

req.body

1 // POST user[name]=tobi&user[email]=tobi@learnboost.com  
2 req.body.user.name  
3 // => "tobi"  
4 req.body.user.email  
5 // => "tobi@learnboost.com"  
6 
7 // POST { "name": "tobi" }  
8 req.body.name  
9 // => "tobi"

req.params

1 // GET /user/tj  
2 req.params.name  
3 // => "tj"  
4 
5 // GET /file/javascripts/jquery.js  
6 req.params[0]  
7 // => "javascripts/jquery.js"  

req.param

 1 **req.param(name)**
 2 
 3 // ?name=tobi  
 4 req.param('name')  
 5 // => "tobi"  
 6 
 7 // POST name=tobi  
 8 req.param('name')  
 9 // => "tobi"  
10 
11 // /user/tobi for /user/:name   
12 req.param('name')  
13 // => "tobi"

req.query:处理get请求

req.body:: 处理 post 请求

req.params: 处理 /:xxx 形式的 get 请求

req.param(): 可以处理 get 和 post 请求,但查找优先级由高到低为req.params→req.body→req.query

原文地址:https://www.cnblogs.com/xujingbo/p/4218641.html