gpt4 book ai didi

node.js - 事件.js :130 throw TypeError ('listener must be a function' )

转载 作者:太空宇宙 更新时间:2023-11-03 22:37:59 25 4
gpt4 key购买 nike

我正在尝试运行一个出现此错误的node.js

events.js:130
throw TypeError('listener must be a function');

我的代码是,

var connect = require("connect");
var http = require("http");
var app = connect();
// Logging middleware
app.use(function(request, response, next) {
console.log("In comes a " + request.method + " to " + request.url);
next();
});

// Send "hello world"
app.use(function(request, response) {
response.writeHead(200, { "Content-Type": "text/plain" });
response.end("Hello world!\n");
});
http.createServer(app).listen(1337);

现在在终端上执行此代码作为 node app.js 我收到错误

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:1850:10)
at Object.exports.createServer (http.js:1880:10)
at Object.<anonymous> (/var/www/express/app.js:17:6)
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)

我安装了node.js和express.js。如何执行这段代码?

最佳答案

我发现了错误。

问题是程序(app.js)不在框架文件夹的根目录中。我将该程序的位置更改为 Express Frameworks 根文件夹。现在可以完美运行了。

关于node.js - 事件.js :130 throw TypeError ('listener must be a function' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21904746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com