2016-10-01 5 views
0

私はこの問題を既に知っていますが、答えは私の問題を解決しませんでした。NodeJS - TypeError:app.listenは関数ではありません

エラーは "TypeError:app.listenは関数ではありません"です。

私の完全なコードは下にあります、ありがとうございます。あなたがそこに持っている

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

こんにちはAdeneoは、私は、NodeJSにかなり新しいです何を意味するのか説明してください。 –

+0

ちょっと混乱しました。最初の行で 'app'を使用しています。ここでsocket.ioに渡しますが、5行目まで定義されていませんか? – adeneo

+0

ドキュメントにいくつかの例がありますが、それらを試しましたか?> https://nodejs.org/api/http.html#http_event_connect – adeneo

答えて

2

このラインから来るエラー:。

app.listen(80); 

あなたのhttpモジュールはvar app = require('http');なので、あなたはノードのhttpモジュールをリッスンしようとしていました(できません)。この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'); 

基本的には、http.createServer()http.serverインスタンスを返します。このインスタンスには、サーバーが指定されたポートで接続を受け入れるようにするlistenメソッドがあります。

だから、これは動作することができます:

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

このことはできません。

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

HTTPモジュールのドキュメント:https://nodejs.org/api/http.html#http_http_createserver_requestlistener

+0

あなたは私の友人が素晴らしいです!これは私のために働いた、多くのありがとう。 –

0

うち明確なすべてのコードを(PSは、私は、同じポート上で実行されているものを持っていない)とlocalhostの訪問後

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

を試してみてください:応答を見て... 8080ページに

しかし、あなたはページのルーティングを処理したい場合は、私はこの作品たら、当時のあなたsocket.ioコードを追加することができ、開始click here for guides ためexpressjsを使用することをお勧めし

1

これは多分SOの質問への答えではありませんが、中同じような場合のテスト同じエラー "TypeError:app.listenは関数ではありません"おそらくモジュール0123をエクスポートすることで解決できます。

$ ./node_modules/.bin/mocha test 

を出力することができる

TypeError: app.listen is not a function 

ソリューション:server.jsファイルの末尾に追加する

試してみてください。

module.exports = app; 
関連する問題