Object没有方法writeHead

时间:2015-08-29 19:44:05

标签: node.js

来自" socket.IO实时Web应用程序开发的示例"来自Rohit Rai。这是关于路由请求的第二个例子:

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

var route = {
        routes:{},
        for: function(path,handler){
            this.routes[path] = handler;
        }
};

route.for("/start", function(response,request){
        response.writeHead(200,{"Content-Type": "text/plain"});
        response.write("Hello2");
        response.end();
    }
);

route.for("/finish", function(response,request){
        response.writeHead(200, {"Content-Type": "text/plain"});
        response.write("Goodbye2");
        response.end();
    }
);

function onRequest(request,response){
    var pathname = url.parse(request.url).pathname;
    console.log("Request for: " + pathname + " received");

    if(typeof route.routes[pathname] === 'function'){
        route.routes[pathname](request,response);

   }
    else{
       response.writeHead(404,{"Content-Type": "text/plain"});
       response.end("404 Not Found!");
   }
};

http.createServer(onRequest).listen(9999);
console.log("Server has started");

控制台说服务器已经启动,404可以工作,但是当我尝试启动或完成时,我在控制台中收到错误消息: 指resources.js:12 TypeError Object#没有方法' writeHead' ???

1 个答案:

答案 0 :(得分:1)

您在/ start和/ finish行中交换了请求和响应参数。

<!DOCTYPE html>
<html ng-app="appModule" ng-controller="controller">

<head>
  <title>this is the title</title>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
</head>

<body>
  <ul>
    <li>{{section.item1}}</li>
    <li>{{section.item2}}</li>
    <li>{{section.item3}}</li>
  </ul>

  <ul>
    <li>{{section.products_section.list_products[0].product_name}}</li>
    <li>{{section.item2}}</li>
    <li>{{section.item3}}</li>
  </ul>

  <div ng-repeat='product in section.products_section.list_products'>
    <directive></directive>
  </div>

  <script src="app.js"></script>
</body>

</html>

这应该可以解决错误。

相关问题