Vinod VT Vinod VT - 22 days ago 7
Node.js Question

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

I am trying to run a node.js getting this error

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


My code is,

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


Now execute this code on terminal as
node app.js
I get an error

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)


I installed node.js and express.js. How to execute this code ?

Answer

I found the mistake.

The problem is that the program(app.js) is not in the root of framework folder. I changed the location of this program to the express frameworks root folder. Now it works perfectly.