[英]How to check if a string matches any of an array of regexes in node.js?
[英]How to check if string = another string in node.js?
所以我使用node.js从API获取信息,然后从API发回响应(主要是为了解决交叉起源和那些有趣的东西),所以我想知道如何检查来自API的响应是某个字符串? 例如,如果你在api中输入一个无效的用户名,它会返回一个空对象,如果你执行object.toString()
它应该是{}
(我想?),所以我想知道如何做一个if语句检查API响应是否为{}
(类似if(object.toString() = "{}" { do stuff }
?)
这是我的代码:
app.get('/stats/id64/:steamid64', function(httpRequest, httpResponse) {
var url = '<api link>&steamid=' +
httpRequest.params.steamid64;
request.get(url, function(error, steamHttpResponse, steamHttpBody) {
httpResponse.setHeader('Content-Type', 'application/json');
var response = {isValidUser: 'false'};
if(steamHttpBody.toString() == "{}") {
httpResponse.send(response);
} else {
httpResponse.send(steamHttpBody);
}
});
});
任何帮助表示赞赏:)
我认为你最好检查一个真正的空对象,而不是将其转换为字符串。 请参见此处: 如何测试空的JavaScript对象?
但是,是的,要比较JavaScript中的字符串,通常会使用===
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.