我对在Node.js中读取文件感到很困惑.
fs.open('./start.html', 'r', function(err, fileToRead){
if (!err){
fs.readFile(fileToRead, {encoding: 'utf-8'}, function(err,data){
if (!err){
console.log('received data: ' + data);
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(data);
response.end();
}else{
console.log(err);
}
});
}else{
console.log(err);
}
});
Run Code Online (Sandbox Code Playgroud)
文件start.html与尝试打开并读取它的文件位于同一目录中.
但是,在控制台中,我得到:
{[错误:ENOENT,打开'./start.html']错误:34,代码:'ENOENT',路径:'./ start.html'}
有任何想法吗?
Eug*_*kov 206
使用path.join(__dirname, '/start.html');
var fs = require('fs'),
path = require('path'),
filePath = path.join(__dirname, 'start.html');
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (!err) {
console.log('received data: ' + data);
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(data);
response.end();
} else {
console.log(err);
}
});
Run Code Online (Sandbox Code Playgroud)
感谢dc5.
Mic*_*ole 37
使用Node 0.12,现在可以同步执行此操作:
var fs = require('fs');
var path = require('path');
// Buffer mydata
var BUFFER = bufferFile('../public/mydata.png');
function bufferFile(relPath) {
return fs.readFileSync(path.join(__dirname, relPath)); // zzzz....
}
Run Code Online (Sandbox Code Playgroud)
fs是文件系统. readFileSync()如果你问的话会返回一个Buffer或string.
fs正确假设相对路径是一个安全问题. path是一种解决方法.
要作为字符串加载,请指定编码:
return fs.readFileSync(path,{ encoding: 'utf8' });
Run Code Online (Sandbox Code Playgroud)
Mas*_*ali 19
1).对于ASync:
var fs = require('fs');
fs.readFile(process.cwd()+"\\text.txt", function(err,data)
{
if(err)
console.log(err)
else
console.log(data.toString());
});
Run Code Online (Sandbox Code Playgroud)
2).对于同步:
var fs = require('fs');
var path = process.cwd();
var buffer = fs.readFileSync(path + "\\text.txt");
console.log(buffer.toString());
Run Code Online (Sandbox Code Playgroud)
Gaj*_*ngh 16
运行此代码,它将从文件中获取数据并显示在控制台中
function fileread(filename)
{
var contents= fs.readFileSync(filename);
return contents;
}
var fs =require("fs"); // file system
var data= fileread("abc.txt");
//module.exports.say =say;
//data.say();
console.log(data.toString());
Run Code Online (Sandbox Code Playgroud)
与节点的简单同步方式:
let fs = require('fs')
let filename = "your-file.something"
let content = fs.readFileSync(process.cwd() + "/" + filename).toString()
console.log(content)
Run Code Online (Sandbox Code Playgroud)
小智 6
使用http模块从服务器读取 html 文件。这是从服务器读取文件的一种方法。如果您想在控制台上获取它,只需删除http模块声明。
var http = require('http');
var fs = require('fs');
var server = http.createServer(function(req, res) {
fs.readFile('HTMLPage1.html', function(err, data) {
if (!err) {
res.writeHead(200, {
'Content-Type': 'text/html'
});
res.write(data);
res.end();
} else {
console.log('error');
}
});
});
server.listen(8000, function(req, res) {
console.log('server listening to localhost 8000');
});Run Code Online (Sandbox Code Playgroud)
<html>
<body>
<h1>My Header</h1>
<p>My paragraph.</p>
</body>
</html>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
187622 次 |
| 最近记录: |