search

Home  >  Q&A  >  body text

node.js - nodejs TypeError: Cannot read property 'url' of undefined

node菜鸟学院node.js路由的学习中提供了一个例子
代码如下:

var http=require('http');
var url=require('url');

function start(route) {
    function onRequest(request,response) {
        var pathname=url.parse(request.url).pathname;
        console.log('Request for'+pathname+'receiver');

        route(pathname);

        response.writeHead(200,{"Content-Type":"text/plain"});
        response.write('Hello Node');
        response.end();
    }
    http.createServer(onRequest()).listen(8888);
    console.log('Server has started');
}
exports.start=start;

当我启动服务的时候提示:

TypeError: Cannot read property 'url' of undefined
    at onRequest (C:\Users\zhengjh\Desktop\test\start.js:6:39)
    at Object.start (C:\Users\zhengjh\Desktop\test\start.js:15:23)
    at Object.<anonymous> (C:\Users\zhengjh\Desktop\test\index.js:3:7)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:968:3

http和url不是nodejs提供的原生模块吗?为什么会出现加载不了的问题,求解

阿神阿神2783 days ago590

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 15:27:29

    The error location is clearly written. It is not that the module does not exist, but that the request.url on line 6 does not exist.
    The reason is written incorrectly below:

    http.createServer(onRequest()).listen(8888);
    =>
    http.createServer(onRequest).listen(8888);
    The parameter here is a callback function rather than a callback function that has been executed.

    reply
    0
  • Cancelreply