Hello world 这样写 为什么会报object is not a function错????
发布于 11 年前 作者 wang1163619 4536 次浏览 最后一次编辑是 8 年前

var http =require(“http”);

var server=http.createServer();

function onRequestCallBack(request,response) { console.log(“Request Received.”); response.writeHead(200,{“Content-type”: “text/plain”}); response.write(“Hello JasonW”); response.end(); }

server(onRequestCallBack).listen(8888);

console.log(“Server has started.”);

6 回复

server(onRequestCallBack).listen(8888); 这一行明显不对吧。callback要么在createServer的时候传递,要么用server.on(‘request’, onRequestCallBack)…

这样吗? 也不对哦!

var http =require(“http”); var server=http.createServer();

function onRequestCallBack(request,response) { console.log(“Request Received.”); response.writeHead(200,{“Content-type”: “text/plain”}); response.write(“Hello JasonW”); response.end; };

server.on(‘request’,onRequestCallBack).listen(8888);

console.log(“Server has started.”);

之前是个问题,这样改了以后不是可以跑了么,end要加括号end()。先找本基本的书,照上面的例子抄一抄吧。

楼主看的是什么书?监听的也是8888 话说,你是不是想写成这样 function start(route, handle) { function onCreate(request, response) { var postData = “”; var pathname = url.parse(request.url).pathname; console.log(“Request for " + pathname + " received.”);

    request.setEncoding("UTF-8");
    request.addListener("data", function(postDataChunk) {
        postData += postDataChunk;
        console.log("Received POST data chunk '"+ postDataChunk + "'.");
    });

    request.addListener("end", function() {
        route(handle, pathname, response, postData);
    });
}
http.createServer(onCreate).listen(8888);
console.log("Server has started");

} exports.start = start; 这是写的例题server的一部分

的确是抄错了,谢谢,下次我一定细心点。

恩恩,共同学习进步

回到顶部