Node.js https.createServer引发TypeError:侦听器必须是一个函数
我已经阅读了所有关于这个post,我知道这一定是愚蠢的,但我不明白为什么下面的代码抛出“ TypeError:侦听器必须是一个函数 ”
假设选项
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
这样的https.createServer
。
我有同样的错误信息:
throw TypeError('listener must be a function');
我有两个单独的文件server.js和handler.js。 我的问题是,当我要求( require(./handler.js
处理程序require(./handler.js
))我的处理程序文件在server.js我不是从handler.js文件导出它。 你必须有: module.exports =handler
; 在您的处理程序文件的底部
- 将https代理configuration为只允许TLS1.2进行传出请求
- Heroku上的HTTPS + SSL – Node + Express
- express-js服务器中的HTTPS SSL损坏(net :: ERR_CERT_COMMON_NAME_INVALID)
- Node.js – 在内部CA中使用https.request()
- 与nodejs和连接的HTTPS
- 如何使用Express for HTTPS的路由?
- 无法使用restify设置localhost https服务器
- 你怎么能得到一个密钥和证书对在https中使用?
- 接收从Node.JS到Azure事件中心的HTTPS请求的ECONNRESET错误