Node.js TypeError:参数'url'必须是字符串,而不是未定义的

时间:2016-10-15 03:41:43

标签: javascript node.js http url

我试图使用url模块和http服务器解析一些网址。

代码如下:

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);

给出以下错误:

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

在这一行:

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

任何人都可以提供帮助?任何解释都将不胜感激。

1 个答案:

答案 0 :(得分:3)

http.IncomingMessage对象的url属性名称为:

req.url

req.URL

因此,req.URLundefined