javascript - how to convert request.url to String - Stack Overflow

For the string, "http:localhost:9090calculator?oper=add&n1=10&n2=20",console.log(u

For the string, "http://localhost:9090/calculator?oper=add&n1=10&n2=20",

console.log(url.parse(req.url).query);
// gives `oper=add&n1=10&n2=20`

whereas

console.log(querystring.parse(req.url));
// gives { '/calculator?oper': 'add', n1: '10', n2: '20' }`

However, I'm not able to split it as these are objects, and not Strings. How do I convert them to strings?

For the string, "http://localhost:9090/calculator?oper=add&n1=10&n2=20",

console.log(url.parse(req.url).query);
// gives `oper=add&n1=10&n2=20`

whereas

console.log(querystring.parse(req.url));
// gives { '/calculator?oper': 'add', n1: '10', n2: '20' }`

However, I'm not able to split it as these are objects, and not Strings. How do I convert them to strings?

Share Improve this question edited Aug 23, 2014 at 1:45 Colin Brock 21.6k9 gold badges49 silver badges62 bronze badges asked Aug 23, 2014 at 1:41 IAMTubbyIAMTubby 1,6874 gold badges28 silver badges42 bronze badges 3
  • 5 I think request.url is already a string, why do you want to convert it to a string? The output of querystring.parse(req.url) is already splited, why do you want to split it? What output are you expecting? – Volune Commented Aug 23, 2014 at 1:53
  • 1 @Volune : Thanks, yes request.url is a string indeed, I was confused, sorry. >The output of querystring.parse(req.url) is already splited But how do I access the zero'th object of this array ?{ '/calculator?oper': 'add', n1: '20', n2: '10' } I can access the n1 and n2 elements as follows. codevar temp = querystring.parse(req.url); console.log(temp.n1); console.log(temp.n2); But as I said, I'm unable to access the zero'th object. PS : I'm having a tough time trying to wrap in code and make bold etc. Can someone point me to a link where I can learn these for Stack Overflow – IAMTubby Commented Aug 23, 2014 at 8:19
  • 1 Please try to clarify your question with what you just said, and fix your question's title. – Volune Commented Aug 23, 2014 at 9:47
Add a ment  | 

2 Answers 2

Reset to default 1

As you can see in what you provided, querystring.parse(req.url) mixed the url path with the key of the zero'th value. This means that querystring.parse should not be called on a full url, but only on the query part of the url. You can do that by using querystring.parse on the result of url.parse:

var parsedUrl = url.parse(testUrl); //would be url.parse(req.url)
var parsedQuery = querystring.parse(parsedUrl.query);

Now you can do parsedQuery.oper, parsedQuery.n1, parsedQuery.n2

Example


If for some reason you want to iterate the parsedQuery (for example if you don't know the keys are oper, n1, n2, ...), you can do it this way:

var parsedQueryKeys = Object.keys(parsedQuery);
parsedQueryKeys.forEach(function(key) {
    console.log( key, parsedQuery[key] );
});

(There are multiple other solutions to iterate an object)


Finally, some libraries automate the parsing for you, so you can directly ask for the query values.

For example, with express's req.param:

console.log( req.param('oper'), req.param('n1'), req.param('n2') );

Find express on npm

Just use object.toString() to convert any variable into a string. Most scalar types and arrays will be converted. Other objects might need to have the method implemented, otherwise they will just return the text [Object] or something.

The second case it looks like console.log() used JSON.stringify(object), probably because the object doesn't have a method toString() implemented.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745359922a4624311.html

相关推荐

  • javascript - how to convert request.url to String - Stack Overflow

    For the string, "http:localhost:9090calculator?oper=add&n1=10&n2=20",console.log(u

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信