最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js https.createServer throws TypeError: listener must be a function - Stack Overflow

programmeradmin4浏览0评论

I've read posts all over concerning this and I know it must be something silly, but I can't figure out why the following code is throwing "TypeError: listener must be a function"

Assume options

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);

Console output:

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)

Can anyone help me figure this out?

I've read posts all over concerning this and I know it must be something silly, but I can't figure out why the following code is throwing "TypeError: listener must be a function"

Assume options

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);

Console output:

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)

Can anyone help me figure this out?

Share Improve this question asked Jun 5, 2014 at 18:53 user3712539user3712539 831 gold badge1 silver badge4 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 19

Where do you assign https? It looks like you’re probably requiring http, not https. http.createServer doesn’t accept options like https.createServer.

You may hit this error when using a node version < 9.6

See the docs and history. I was very confused that the docs said I could use an options object on http.createServer and got this error until I realized I hadn't updated node in a while.

https://nodejs.org/api/http.html#http_http_createserver_options_requestlistener

I had the same error message:

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

I have two separate files server.js and handler.js. My problem was whilst I was requiring ( require(./handler.js) )my handler file in server.js I was not exporting it from handler.js file. You must have: module.exports =handler; at the bottom of your handler file

This is mostly coming from a version mismatch. Latest versions of nodejs's http.createServer() do take options in parameters like https does.

发布评论

评论列表(0)

  1. 暂无评论