0
var http = require('http');
var fs = require('fs');
var path="";
process.stdin.on('data', function(chunk) {
var buffer = new Buffer(chunk);
path = buffer.toString();
path = path.replace("\n","");
path = path.replace("\r","");
});
var str="";
var fileRead;
var arrayFiles = [];
function onRequest(request, response) {
str = "";
if (request.url === '/favicon.ico') {
response.writeHead(200, {'Content-Type': 'image/x-icon'});
response.end();
return;
} else if (request.url === '/index.html') {
console.log("Request received" + path);
fs.readdir(path, function(err, items) {
console.log(items);
arrayFiles = [];
str += items;
arrayFiles.push(items);
console.log("Enter file to be read");
process.stdin.on('data', function(chunk) {
var buffer = new Buffer(chunk);
fileRead = buffer.toString();
fileRead = fileRead.replace("\n","");
fileRead = fileRead.replace("\r","");
if(arrayFiles[0].indexOf(fileRead) != -1) {
fs.readFile(fileRead, 'utf8', function(err, contents) {
response.writeHead(200, {"Context-Type": "text/plain"});
response.write(contents);
response.end();
});
}
});
});
}
}
http.createServer(onRequest).listen(8000);
給定的程序顯示在作爲輸入給出的目錄中的文件。然後輸入要顯示其內容的文件名。http沒有快遞的GET請求
在給定的程序中,我需要使用GET請求的用戶輸入文件的名稱,而不是從標準輸入讀取值。沒有快速模塊可以做到這一點嗎?如果是的話,請幫助。
在此先感謝
,該文件的「文件名」?正在執行的node.js代碼文件?或者用戶上傳的文件? – shaochuancs
要顯示內容的文件@shaochuancs –
我想你可以把文件名作爲URL參數,例如'GET /index.html?filename = test.txt' – shaochuancs