req.params.number是expressjs中的字符串吗? [英] req.params.number is string in expressjs?

查看:97
本文介绍了req.params.number是expressjs中的字符串吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写expressjs应用程序.是req.params.anything始终是字符串而不是数字 假设我为user_id传递了一个数字,它的typeof总是字符串.

I am writing expressjs app. is req.params.anything always string and not number suppose if I pass a number for user_id it's typeof is always string.

app.get('user/:user_id', function(req, res){
  console.log(typeof req.params.user_id);
});

GET用户/21

此日志字符串.

那么是否总是为req.params.x输入字符串?

So is it always type string for req.params.x?

推荐答案

是的,所有参数都是字符串.

Yes, all params will be strings.

这是从expressjs route.js中提取的:

This is extracted from the expressjs route.js:

var val = 'string' == typeof m[i]
  ? decodeURIComponent(m[i])
  : m[i];

所以val始终是字符串,因为decodeURIComponent的结果始终是字符串,而m是RegExp.exec()的结果,该函数返回匹配的字符串数组,因此还可以安全地假设m[i]将是一个字符串.

So the val will always be a string, since the result of decodeURIComponent is always a string, while m is the result of a RegExp.exec() which returns an array of matched strings, so it's also safe to assume that m[i] will be a string.

这篇关于req.params.number是expressjs中的字符串吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆