찾다

 >  Q&A  >  본문

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提供的原生模块吗?为什么会出现加载不了的问题,求解

阿神阿神2864일 전634

모든 응답(1)나는 대답할 것이다

  • PHP中文网

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

    오류 위치가 명확하게 적혀있습니다. 모듈이 없는 것이 아니라 6행의 request.url이 존재하지 않는 것입니다.
    아래에 이유가 잘못 적혀 있습니다.

    http.createServer(onRequest()).listen(8888);
    =>
    http.createServer(onRequest).listen(8888);
    여기서의 매개변수는 실행된 콜백 함수가 아닌 콜백 함수입니다.

    회신하다
    0
  • 취소회신하다