最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js TypeError: Parameter 'url' must be a string, not undefined - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question asked Oct 15, 2016 at 3:41 JacsJacs 1,5374 gold badges22 silver badges33 bronze badges 1
  • 1 try using req.url – Anuj Commented Oct 15, 2016 at 4:19
Add a ment  | 

1 Answer 1

Reset to default 3

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

req.url

not

req.URL

thus, req.URL is undefined.

发布评论

评论列表(0)

  1. 暂无评论