【问题标题】:Node.js TypeError: Parameter 'url' must be a string, not undefinedNode.js TypeError:参数'url'必须是字符串,而不是未定义
【发布时间】:2017-02-24 13:37:19
【问题描述】:

我正在尝试使用 url 模块和 http 服务器解析一些 url。

代码如下:

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;

有人可以帮忙吗?任何解释将不胜感激。

【问题讨论】:

  • 尝试使用req.url

标签: javascript node.js http url


【解决方案1】:

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

req.url

不是

req.URL

因此,req.URLundefined

【讨论】:

    猜你喜欢
    • 2018-05-16
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 2017-12-04
    • 2023-02-18
    • 2017-09-13
    • 2017-01-28
    • 1970-01-01
    相关资源
    最近更新 更多