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.
The url property name for an http.IncomingMessage object is:
req.url
not
req.URL
thus, req.URL is undefined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With