2

I'm trying to get parameters from a URL, for example:

http://localhost:8888/?name=test

To get name parameter I saw some samples where they use the url module like this:

var url = require('url');

var urlParts = url.parse(request.url, true);
var query = urlParts.query;

So, first I ran this command npm install url, also the dependency is on the package.json file, but I always get this error:

TypeError: Cannot call method 'parse' of undefined
at C:\Users\Administrator\git\test\app.js:28:7

Anyone has faced this problem before?

2
  • What is the output of console.log(url);? Commented Aug 20, 2014 at 20:54
  • I found the problem (see comment below), thanks for your time :) Commented Aug 20, 2014 at 21:02

3 Answers 3

12

I found the problem, I had the code like this:

var http = require("http");
var url = require('url');

http.createServer(function(request, response) {
    var urlParts = url.parse(request.url, true);
    var query = urlParts.query;
}).listen(appport);

And the url object was not accessible inside the createServer function (not sure why), so I just replace this line:

var urlParts = url.parse(request.url, true);

with this:

var url_urlParts = require('url').parse(request.url, true);

and now is working fine.

Sign up to request clarification or add additional context in comments.

3 Comments

Did you perhaps have a second var url inside the createServer callback somewhere?
My only concern with your solution is that require is one of few synchronous methods in node (it was a nightmare when it wasn't apparently) so you're making a blocking call in handler. This may or may not be an issue, because it's also pretty smart and caches requested files. Just might want to verify that.
Thanks John, I didn't know about that, I'm going to take a closer look at this
2

This one bit me too - my problem was that url.parse was conflicting with a local var url. I solved it like this:

import { parse as urlparse } from 'url'
var url = 'https://localhost:80'
var parsed_url = urlparse(url)

Thanks @loganfsmyth for the insight!

Comments

-1

function parse(urlStr: string, parseQueryString: true, slashesDenoteHost?: boolean): UrlWithParsedQuery (+3 overloads) @deprecated — since v11.0.0 - Use the WHATWG URL API.

'(urlStr: string, parseQueryString: true, slashesDenoteHost?: boolean): UrlWithParsedQuery' is deprecated.ts(6385) url.d.ts(51, 9): The declaration was marked as deprecated here.

1 Comment

DEP0116: Legacy URL API# History Version Changes v15.13.0 Deprecation revoked. Status changed to "Legacy". v11.0.0 Documentation-only deprecation. Type: Deprecation revoked The Legacy URL API is deprecated. This includes url.format(), url.parse(), url.resolve(), and the legacy urlObject. Please use the WHATWG URL API instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.