2016-10-15 88 views
1

我试图解析一些使用url模块和http服务器的URL。Node.js TypeError:参数'url'必须是一个字符串,而不是undefined

代码波纹管:

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

尝试使用'req.url' – SpunkyLive

回答

相关问题