function onRequest(request, response) { console.log(“Request received.”); response.writeHead(200, {“Content-Type”: “text/plain”}); response.write(“Hello World”); response.end(); }
http.createServer(onRequest).listen(8888); console.log(“Server has started.”);
这样是可以的,但是加了粗字体的后 var http = require(“http”);
function start() { function onRequest(request, response) { console.log(“Request received.”); response.writeHead(200, {“Content-Type”: “text/plain”}); response.write(“Hello World”); response.end(); }
http.createServer(onRequest).listen(8888); console.log(“Server has started.”); } exports.start = start; 就
怎么办?
粗体字 表明你把一系列的操作包裹在一个start函数里面 ,并开放(exports)了一个端口供外部调用。 但是最重要的是你没有执行这个start
那该怎么执行这个start呢?
@jackchen 你可以重新建一个文件,里面写: var start = require(’./server.js’).start; start();