最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NodeJS - TypeError: app.listen is not a function - Stack Overflow

programmeradmin1浏览0评论

I know this question kind of already exists, but they answers did not rectify my problem.

The error is "TypeError: app.listen is not a function";

My full code is below, thanks in advance. (PS, I do not have anything running on the same port)

var io = require('socket.io')(app);
var fs = require('fs');
var serialPort = require("serialport");

var app = require('http');

app.createServer(function (req, res) {
    fs.readFile(__dirname + '/index.html',
        function (err, data) {
            res.writeHead(200);
            res.end(data);
        });
}).listen(1337, '127.0.0.1');


var port = new serialPort(process.platform == 'win32' ? 'COM3' : '/dev/ttyUSB0', {
    baudRate: 9600
});

port.on( 'open', function() {
    console.log('stream read...');
});

port.on( 'end', function() {
    console.log('stream end...');
});

port.on( 'close', function() {
    console.log('stream close...');
});

port.on( 'error', function(msg) {
    console.log(msg);
});

port.on( 'data', function(data) {
    console.log(data);

    var buffer = data.toString('ascii').match(/\w*/)[0];
    if(buffer !== '') bufferId += buffer;

    clearTimeout(timeout);
    timeout = setTimeout(function(){
        if(bufferId !== ''){
            id = bufferId;
            bufferId = '';
            socket.emit('data', {id:id});
        }
    }, 50);
});

io.on('connection', function (socket) {
    socket.emit('connected');
});

app.listen(80);

I know this question kind of already exists, but they answers did not rectify my problem.

The error is "TypeError: app.listen is not a function";

My full code is below, thanks in advance. (PS, I do not have anything running on the same port)

var io = require('socket.io')(app);
var fs = require('fs');
var serialPort = require("serialport");

var app = require('http');

app.createServer(function (req, res) {
    fs.readFile(__dirname + '/index.html',
        function (err, data) {
            res.writeHead(200);
            res.end(data);
        });
}).listen(1337, '127.0.0.1');


var port = new serialPort(process.platform == 'win32' ? 'COM3' : '/dev/ttyUSB0', {
    baudRate: 9600
});

port.on( 'open', function() {
    console.log('stream read...');
});

port.on( 'end', function() {
    console.log('stream end...');
});

port.on( 'close', function() {
    console.log('stream close...');
});

port.on( 'error', function(msg) {
    console.log(msg);
});

port.on( 'data', function(data) {
    console.log(data);

    var buffer = data.toString('ascii').match(/\w*/)[0];
    if(buffer !== '') bufferId += buffer;

    clearTimeout(timeout);
    timeout = setTimeout(function(){
        if(bufferId !== ''){
            id = bufferId;
            bufferId = '';
            socket.emit('data', {id:id});
        }
    }, 50);
});

io.on('connection', function (socket) {
    socket.emit('connected');
});

app.listen(80);
Share Improve this question asked Oct 1, 2016 at 19:27 Peter BennettPeter Bennett 1941 gold badge3 silver badges15 bronze badges 3
  • Hi Adeneo, I am fairly new to NodeJS, please explain what you mean? – Peter Bennett Commented Oct 1, 2016 at 19:31
  • I just got a little confused, you're using app on the first line, where you pass it to socket.io, yet it's not defined until the fifth line ? – adeneo Commented Oct 1, 2016 at 19:33
  • There are some examples in the docs, did you try those -> nodejs.org/api/http.html#http_event_connect – adeneo Commented Oct 1, 2016 at 19:34
Add a comment  | 

3 Answers 3

Reset to default 10

This is maybe not an answer to SO question, but in a similar case with test the same error "TypeError: app.listen is not a function" probably can be solved by exporting module app.

$ ./node_modules/.bin/mocha test

can output

TypeError: app.listen is not a function

Solution:

Try adding to the bottom of the server.js file:

module.exports = app;

the error come from this line :

app.listen(80);

Since app is your http module var app = require('http');, you were trying to listen to the node http module (and well you can't). You need to create a server with this http module and then listen to it.

This is what you did with those lines :

app.createServer(function (req, res) {
    fs.readFile(__dirname + '/index.html',
        function (err, data) {
            res.writeHead(200);
            res.end(data);
        });
}).listen(1337, '127.0.0.1');

Basically, http.createServer() returns a http.server instance. This instance has a listen method that causes the server to accept connections on the specified port.

So this can work :

var app = require('http');
app.createServer().listen(8080);

this can't :

var app = require('http');
app.listen(8080);

The http module documentation : https://nodejs.org/api/http.html#http_http_createserver_requestlistener

clear out all the codes you have there and try

const http = require('http');
const handleRequest = (request, response) => {
  console.log('Received request for URL: ' + request.url);
  response.writeHead(200);
  response.end('Hello World!');
};

const www = http.createServer(handleRequest);
www.listen(8080);

Then visit localhost:8080 ...to see a response to the page.

But if you want to handle page routing, i suggest using expressjs for a start click here for guides Once this works, you can then add your socket.io codes back in.

发布评论

评论列表(0)

  1. 暂无评论