Reputation: 1537
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.
Upvotes: 3
Views: 29468
Reputation: 707158
The url property name for an http.IncomingMessage
object is:
req.url
not
req.URL
thus, req.URL
is undefined
.
Upvotes: 3