node . js https.createServer抛出TypeError: listener必须是一个函数
Node.js https.createServer throws TypeError: listener must be a function
我读过所有关于这个的帖子,我知道它一定是愚蠢的,但我不明白为什么下面的代码会抛出"TypeError: listener必须是一个函数"
假设选项
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);
控制台输出
:
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)
有谁能帮我解决这个问题吗? 您将https
分配到哪里?看起来你可能需要http
,而不是https
。http.createServer
不接受https.createServer
等选项
使用节点版本时可能会遇到此错误<9.6
查看文档和历史。我非常困惑的是,文档说我可以在http上使用选项对象。createServer和得到这个错误,直到我意识到我没有更新节点在一段时间。
https://nodejs.org/api/http.html http_http_createserver_options_requestlistener
我有相同的错误信息:
throw TypeError('listener must be a function');
require(./handler.js
))我的处理程序文件在服务器.js我没有导出它从处理程序.js文件。你必须有:module.exports =handler
;在处理程序文件
这主要是由于版本不匹配。最新版本的nodejs的http.createServer()
像https一样在参数中接受选项。
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 我可以在json对象中添加一个函数吗
- Javascript使函数一个接一个地执行
- 是JavaScript中的函数一个对象
- 两个几乎相等的jQuery函数;一个适用于IE,一个不适用于IE
- 两个Javascript函数一个window.onload=Custom.init;和一个window.onload=f
- 使javascript加载函数一个接一个地执行
- 如何通过两个嵌套的匿名函数(一个带有超时的事件处理程序)传递变量
- 为什么我不能让两个jQuery函数一个在另一个里面呢?
- 给函数一个变量名,而不是它的值
- 如何使用嵌入函数(一个滚动页由Pete - peachananr)
- 是否有可能给JavaScript函数一个类型/类?
- 确保两个函数一个接一个地执行,其中第一个函数内部有一个异步调用
- 两个不同的ajax函数一个接一个调用返回相同的值
- jQuery第一次更改函数一个工作
- 给这个函数一个id
- 如何给回调函数一个值一个变量在特定时刻
- 使用回调使两个函数一个接一个地运行
- 两个函数(一个用php)是否可以用“;onclick”;
- 两个几乎相同的函数.一个函数用Queue进行排队.Jquery效果在queued函数上不起作用.为什么