3

Im trying to parse some urls using url module and http server.

Code bellow:

var http = require('http');
var URL = require('url');
var port = 8080;

var server = http.createServer(function(req, res) {
    var parsedURL = URL.parse(req.URL, true).pathname;
    switch(parsedURL) {
        case 'test/myurl':
            console.log('Valid URL.');
            break;
        default:
            console.log('404!')
    }
});

server.listen(port);
console.log('Service at port: ' + port);

gives following error:

TypeError: Parameter 'url' must be a string, not undefined

at this line:

var parsedURL = URL.parse(req.URL, true).pathname;

Anyone can help? Any explanation would be appreciated.

1
  • 1
    try using req.url Commented Oct 15, 2016 at 4:19

1 Answer 1

3

The url property name for an http.IncomingMessage object is:

req.url

not

req.URL

thus, req.URL is undefined.

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

Comments

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.