2013-09-05 27 views
0

我使用fs庫從系統讀取文件。我不知道我使用這個庫,並遇到錯誤。fs library:使用時出錯

  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.listen (net.js:1127:5) at Object. (/home/hqt/PhpstormProjects/NodeApp/sample/AsynchronouslyReading.js:21:4) 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)

這裏是我的代碼:

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機器進行開發。

謝謝:)

回答

2

這是因爲別的東西已經在8124端口上偵聽的Linux,你可以使用像netstat -tanp | grep LISTEN | grep 8124看到的。

+0

啊。謝謝:)這是真的。但是在我運行這個文件後,我按下Control + Z來停止服務器。 (並且我再次檢查過,例如:localhost:8124,不能,這意味着我的服務器已停止)。但港口仍在使用中。如何從控制檯正確地停止節點服務器?謝謝:) – hqt

+3

@hqt,Control + C. Control + Z只暫停進程,不停止它(你可以用'fg'將它帶回前臺)。 – rid

+0

啊。 sooo謝謝:D我是Linux世界的新來者,我按下Ctrl + Z,它終止了。 (我不知道背後有什麼)。非常感謝你幫助我:D – hqt