最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论