0

fsシステムからファイルを読み取るためにライブラリを使用します。このライブラリを使用してエラーが発生するかどうかわかりません。

  1. 私はPhpStormを使用しています。行 : fs.readFile(): その下に、未解決の関数またはメソッド readFile() に気付く行があります。これは、IDE がこの関数の場所を認識していないことを意味します。それでも、私はチェックfs.jsしましたが、問題はありません。

  2. 実行中に次のエラーが表示されます。

events.js:72 throw er; // Unhandled 'error' event ^ Error: listen EADDRINUSE at errnoException (net.js:901:11) at Server._listen2 (net.js:1039:14) at listen (net.js:1061:10) at Server.オブジェクトでリッスン (net.js:1127:5) します。(/home/hqt/PhpstormProjects/NodeApp/sample/AsynchronouslyReading.js:21:4) Module._compile (module.js:456:26) で Object.Module._extensions..js (module.js:474:10) ) Function.Module._load (module.js:312:12) で Module.load (module.js:356:32) で Function.Module.runMain (module.js:497:10) で

これが私のコードです:

var http = require('http');
var fs = require('fs');

// create http server
http.createServer(function(req, res) {
    fs.readFile('helloworld.js', 'utf-8', function(err, data) {
        res.writeHead(200, {'content-type' : 'text/plain'});
        if (err) res.write('could not find or open file');
        else res.write(data);

        // ending
        res.end();
    });
}).listen(8124, function() {console.log('server is running at 8124');});

console.log('server is runnint at 8124 port');

理由を理解するのを手伝ってください。開発にはUbuntuマシンを使用しています。

ありがとう :)

4

1 に答える 1

2

これは、別の何かがポート 8124 で既にリッスンしているためです。Linux では、次のようなものを使用して何netstat -tanp | grep LISTEN | grep 8124を確認できます。

于 2013-09-05T12:14:59.100 に答える