Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
44
rated 0 times [  45] [ 1]  / answers: 1 / hits: 29005  / 8 Years ago, sat, october 15, 2016, 12:00:00

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.


More From » node.js

 Answers
7

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



req.url


not



req.URL


thus, req.URL is undefined.


[#60388] Thursday, October 13, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
shelbiec

Total Points: 101
Total Questions: 106
Total Answers: 106

Location: Ivory Coast
Member since Fri, Oct 8, 2021
3 Years ago
;