0
var http = require('http');
var fs = require('fs');
var path = process.argv[2];
var str="";
function onRequest(request, response) {
str += "";
console.log("Request received" + path);
fs.readdir(path, function(err, items) {
str += items;
});
response.writeHead(200, {"Context-Type": "text/plain"});
response.write(new Buffer(str).toString());
response.end();
}
http.createServer(onRequest).listen(8000);
上面的代碼片段創建一個http服務器,該服務器從用戶獲取目錄路徑作爲參數。發出http請求以獲取目錄中可用文件的列表並將其作爲響應發回。僅在第二個請求中寫入頁面的響應
只有在第二個請求中才將響應寫入頁面。該頁面在第一個請求中顯示爲空。
任何人都可以幫忙。提前致謝!!
如何使用標準輸入讀取上述代碼的輸入 –
stdin有點複雜,因爲您不知道是否所有的stdin都讀取。看看https://gist.github.com/kristopherjohnson/5065599或https://www.npmjs.com/package/get-stdin – Julian
process.stdin.on('readable',() => { path = process.stdin.read(); console.log(path); });無法閱讀使用stdin –