javascript Node.js https.createServer 抛出 TypeError:监听器必须是一个函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24068035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:06:49  来源:igfitidea点击:

Node.js https.createServer throws TypeError: listener must be a function

javascriptnode.jshttps

提问by user3712539

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"

我已经阅读了所有关于此的帖子,我知道这一定很愚蠢,但我不明白为什么下面的代码会抛出“ 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?

谁能帮我解决这个问题?

回答by Todd Yandell

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

你分配在哪里https?看起来您可能需要http,而不是httpshttp.createServer不接受像https.createServer.

回答by Colten Hymanson

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

使用节点版本 < 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.

查看文档和历史。我很困惑,文档说我可以在 http.createServer 上使用选项对象并收到此错误,直到我意识到我有一段时间没有更新节点。

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

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

回答by Semtex

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

这主要来自版本不匹配。最新版本的 nodejshttp.createServer()确实像 https 一样在参数中采用了选项。

回答by Tormod Smith

I had the same error message:

我有同样的错误信息:

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

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

我有两个单独的文件 server.js 和 handler.js。我的问题是当我require(./handler.js在 server.js 中需要 ( ) ) 我的处理程序文件时,我没有从 handler.js 文件中导出它。你必须有: module.exports =handler; 在处理程序文件的底部