検索

ホームページ  >  に質問  >  本文

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

阿神阿神2871日前646

全員に返信(1)返信します

  • PHP中文网

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

    モジュールが存在しないのではなく、6行目のrequest.urlが存在しないというエラー箇所が明記されています。
    以下の理由は間違って書かれています:

    http.createServer(onRequest()).listen(8888);
    =>
    http.createServer(onRequest).listen(8888);
    ここでのパラメータは、実行されたコールバック関数ではなく、コールバック関数です。

    返事
    0
  • キャンセル返事