2014-06-05 22 views
5

Tôi đã đọc tất cả các bài viết liên quan đến điều này và tôi biết nó phải là một cái gì đó ngớ ngẩn, nhưng tôi không thể hiểu tại sao đoạn mã sau được ném "Lỗi Loại: người nghe phải có một hàm "Node.js https.createServer ném TypeError: người nghe phải là một hàm

Giả tùy chọn

var server = https.createServer(options, function(request,response){ 
if (request.url==='/') request.url='/home/altronic/Opti-Cal/web/arimonitor.htm'; 
console.log("Request: " + request.url); 
fs.readFile("public"+request.url,function(error,data){ 
    if (error) { 
     response.writeHead(404, {"Content-type":"text/plain"}); 
     response.end ("Sorry the page you requested was not found."); 
    } else { 
     response.writeHead(200,{"Content-type":mime.lookup('public'+request.url)}); 
     response.end (data); 

      } 
}) 
}).listen(port); 

điều khiển đầu ra:

events.js:130 
throw TypeError('listener must be a function'); 
    ^
TypeError: listener must be a function 
at TypeError (<anonymous>) 
at Server.EventEmitter.addListener (events.js:130:11) 
at new Server (http.js:1816:10) 
at Object.exports.createServer (http.js:1846:10) 
at Object.<anonymous> (/home/altronic/Opti-Cal/src/Opti-Cal_HTTPS_Server.js:42:20) 
at Module._compile (module.js:456:26) 
at Object.Module._extensions..js (module.js:474:10) 
at Module.load (module.js:356:32) 
at Function.Module._load (module.js:312:12) 
at Function.Module.runMain (module.js:497:10) 

bất cứ ai có thể giúp tôi hiểu ra điều này?

Trả lời

17

Nơi nào bạn gán https? Có vẻ như bạn có thể yêu cầu http, chứ không phải https. http.createServer không chấp nhận các tùy chọn như https.createServer.

+0

+1 Tôi nghĩ đó chính là điều đang xảy ra. Nó sẽ mang lại chính xác cùng một lỗi. –

+1

Dấu vết ngăn xếp hiển thị khung trong 'http.js', vì vậy điều này có vẻ khá khả thi. –

+0

Cảm ơn bạn. Đó chính xác là vấn đề! – user3712539

0

tôi đã được thông báo lỗi tương tự:

throw TypeError('listener must be a function');

Tôi có hai tập tin server.js và handler.js riêng biệt. Vấn đề của tôi là khi tôi đang yêu cầu (require(./handler.js)) tệp xử lý của tôi trong server.js Tôi đã không xuất tệp đó từ tệp handler.js. Bạn phải có: module.exports =handler; ở cuối tệp trình xử lý của bạn

Các vấn đề liên quan