本文翻译自:How to access the GET parameters after “?” in Express?
I know how to get the params for queries like this: 我知道如何为这样的查询获取参数:
app.get('/sample/:id', routes.sample);
In this case, I can use req.params.id
to get the parameter (eg 2
in /sample/2
). 在这种情况下,我可以使用req.params.id
来获得参数(例如2
中/sample/2
)。
However, for url like /sample/2?color=red
, how can I access the variable color
? 但是,对于像/sample/2?color=red
这样的url,如何访问变量color
?
I tried req.params.color
but it didn't work. 我尝试了req.params.color
但是没有用。
#1楼
参考:https://stackoom.com/question/19MYX/如何在Express中的-之后访问GET参数
#2楼
So, after checking out the express reference , I found that req.query.color
would return me the value I'm looking for. 因此,在检查了快速参考之后 ,我发现req.query.color
将返回我想要的值。
req.params refers to items with a ':' in the URL and req.query refers to items associated with the '?' req.params是指URL中带有“:”的项目,而req.query是指与“?”相关的项目。
Example: 例:
GET /something?color1=red&color2=blue
Then in express, the handler: 然后,快递员:
app.get('/something', (req, res) => {
req.query.color1 === 'red' // true
req.query.color2 === 'blue' // true
})
#3楼
Update: req.param()
is now deprecated, so going forward do not use this answer. 更新:现在不推荐使用req.param()
,因此不要使用此答案。
Your answer is the preferred way to do it, however I thought I'd point out that you can also access url, post, and route parameters all with req.param(parameterName, defaultValue)
. 您的答案是执行此操作的首选方法,但是我想指出的是,您还可以使用req.param(parameterName, defaultValue)
来访问url,post和route参数。
In your case: 在您的情况下:
var color = req.param('color');
From the express guide: 从快速指南中:
lookup is performed in the following order: 查找按以下顺序执行:
- req.params 需求参数
- req.body 需求主体
- req.query 请求查询
Note the guide does state the following: 请注意,该指南确实指出以下内容:
Direct access to req.body, req.params, and req.query should be favoured for clarity - unless you truly accept input from each object. 为了清楚起见,应该倾向于直接访问req.body,req.params和req.query-除非您真正接受每个对象的输入。
However in practice I've actually found req.param()
to be clear enough and makes certain types of refactoring easier. 但是在实践中,我实际上发现req.param()
足够清楚,并使某些类型的重构更加容易。
#4楼
@Zugwait's answer is correct. @Zugwait的答案是正确的。 req.param()
is deprecated. req.param()
已弃用。 You should use req.params
, req.query
or req.body
. 您应该使用req.params
, req.query
或req.body
。
But just to make it clearer: 但只是为了使它更清楚:
req.params
will be populated with only the route values. req.params
仅填充路由值。 That is, if you have a route like /users/:id
, you can access the id
either in req.params.id
or req.params['id']
. 也就是说,如果您具有/users/:id
类的路由,则可以在req.params.id
或req.params['id']
访问id
。
req.query
and req.body
will be populated with all params, regardless of whether or not they are in the route. req.query
和req.body
将填充所有参数,无论它们是否在路径中。 Of course, parameters in the query string will be available in req.query
and parameters in a post body will be available in req.body
. 当然,查询字符串中的参数将在req.query
可用,而帖子正文中的参数将在req.body
可用。
So, answering your questions, as color
is not in the route, you should be able to get it using req.query.color
or req.query['color']
. 因此,回答您的问题,因为color
不在路线中,您应该可以使用req.query.color
或req.query['color']
来获取req.query['color']
。
#5楼
The express manual says that you should use req.query to access the QueryString. 快递手册说您应该使用req.query来访问QueryString。
// Requesting /display/post?size=small
app.get('/display/post', function(req, res, next) {
var isSmall = req.query.size === 'small'; // > true
// ...
});
#6楼
Query string and parameters are different. 查询字符串和参数不同。
You need to use both in single routing url 您需要在单个路由网址中同时使用
Please check below example may be useful for you. 请检查以下示例可能对您有用。
app.get('/sample/:id', function(req, res) {
var id = req.params.id; //or use req.param('id')
................
});
Get the link to pass your second segment is your id example: http://localhost:port/sample/123 获取链接以传递您的第二个片段是您的id示例: http:// localhost:port / sample / 123
If you facing problem please use Passing variables as query string using '?' 如果您遇到问题,请使用“?”将变量作为查询字符串传递 operator 算子
app.get('/sample', function(req, res) {
var id = req.query.id;
................
});
Get link your like this example: http://localhost:port/sample?id=123 获取链接,例如以下示例: http:// localhost:port / sample?id = 123
Both in a single example 两者都在一个示例中
app.get('/sample/:id', function(req, res) {
var id = req.params.id; //or use req.param('id')
var id2 = req.query.id;
................
});
Get link example: http://localhost:port/sample/123?id=123 获取链接示例: http:// localhost:port / sample / 123?id = 123