我有以下代码:
server.js:
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;
index.js:
var server = require('./server');
server.start();
我尝试将server.js变成一个揭示模块模式:
var http = require("http");
var server = function() {
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.");
}
return {
start: start
};
}();
但我收到错误:TypeError: server.start is not a function
我犯了什么错误?
感谢。
答案 0 :(得分:2)
您忘记导出模块
exports = server;