在index.js中,我能够在控制台中获取数据。但我正在尝试在浏览器中显示它。如何将数据从index.js发送到html页面?有谁可以帮我解决...
我在index.js中的代码:
walk(dirctory, function(filePath, stat) {
console.log(filePath); //able to get data
$("#table").val(filePath);
});
我的HTML:
<script src="javascripts/script.js"></script>
<script src="javascripts/jquery-1.11.2.min.js"></script>
<div id="box">
<h2>DOWNLOAD</h2>
<h> Download MLabs ".tgz" files for one month </h>
<div id="text">
<form id="download" method="post" action="/download">
<center>
<span class ="mandatory" style="color:red">*</span>
<label>Year : <select id ="year" name="year">
<option value="select year:" selected>Select year:</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
</select>
</label></br></br>
<span class ="mandatory" style="color:red">*</span>
<label>Month : <select id ="month" name="month">
<option value="select month:" selected>Select month:</option>
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
</label></br></br>
<span class ="mandatory" style="color:red">*</span>
<label>Number of files to download : <select id ="files" name="files">
<option value="select value:" selected>Select value:</option>
<option value="1">1</option>
<option value="100">100</option>
<option value="150">150</option>
<option value="200">200</option>
<option value="ALL">ALL</option>
</select>
</label></br></br>
<input type="submit" name="submit" onclick="createFormElement()" value="Download" /> <input type="reset" value="Reset" />
</center>
</form>
</div>
</div>
<div id="fileDiv"></div>
</script>
我的index.js:
router.post('/download', function(req, res, next) {
var year = req.body.year;
var month = req.body.month;
var limit = req.body.files;
function walk(currentDirPath, callback) {
var fs = require('fs'), path = require('path');
fs.readdirSync(currentDirPath).forEach(function(name) {
var filePath = path.join(currentDirPath, name);
var stat = fs.statSync(filePath);
if (stat.isFile()) {
callback(filePath, stat);
} else if (stat.isDirectory()) {
walk(filePath, callback);
}
});
}
var dirctory ="/usr/local/ndt-files"+"/"+year+"/"+month;
walk(dirctory, function(filePath, stat) {
console.log(filePath);
$("#table").val(filePath);
});
var stream = fs.createWriteStream('views/downloadInput.txt');
stream.once('open', function(fd) {
stream.write(year);
stream.write('/');
stream.write(month);
//stream.write('/');
//stream.write(limit);
stream.end();
});
child = exec("/usr/local/src/node-v0.10.35/nodeprograms/views/scriptformonth.sh",
function (error, stdout, stderr) {
if (!error) {
console.log(stdout);
} else {
res.send(error);
}
});
});
答案 0 :(得分:0)
将Web服务器视为接收请求作为输入并输出响应的黑盒子。当您使用Web浏览器向Web服务器发送HTTP请求时,Web浏览器将收到响应。
因此,要回答您的问题,在服务器端,您无需将响应发送给特定客户端。只需使用res.send('whatever')
,无论客户提出请求都会得到响应。
答案 1 :(得分:0)
当您使用res.send(error)
发生错误并完全忽略其他方案时,您只会将响应发送回客户端。您还需要使用res.send(..)
发送输出。
在代码中进行以下更改 -
function (error, stdout, stderr) {
if (!error) {
console.log(stdout);
// send the output here -
res.send(stdout);
} else {
res.send(error);
}
});