我正在关注:The Node Beginner Book
在使用另一个SO帖子中的代码进行测试后:
var Fs = require('fs');
var dirs = ['tmp'];
var index;
var stats;
for (index = 0; index < dirs.length; ++index)
{
try
{
stats = Fs.lstatSync(dirs[index]);
console.log(dirs[index] + ": is a directory? " + stats.isDirectory());
}
catch (e)
{
console.log(dirs[index] + ": " + e);
}
}错误仍然存在:
错误: ENOENT,没有这样的文件或目录'tmp‘

tmp上的权限为777。
requestHandlers.js
var querystring = require("querystring"),
fs = require("fs");
function start(response, postData) {
console.log("Request handler 'start' was called.");
var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'<style>input{display: block; margin: 1em 0;}</style>'+
'</head>'+
'<body>'+
'<form action="/upload" method="post">'+
'<textarea name="text" rows="20" cols="60"></textarea>'+
'<input type="submit" value="Submit text" />'+
'</form>'+
'</body>'+
'</html>';
response.writeHead(200, {"Content-Type": "text/html"});
response.write(body);
response.end();
}
function upload(response, postData) {
console.log("Request handler 'upload' was called.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("You've sent the text: "+
querystring.parse(postData).text);
response.end();
}
function show(response, postData) {
console.log("Request handler 'show' was called.");
fs.readFile("/tmp/test.jpg", "binary", function(error, file) {
if(error) {
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(error + "\n");
response.end();
} else {
response.writeHead(200, {"Content-Type": "image/jpg"});
response.write(file, "binary");
response.end();
}
});
}
exports.start = start;
exports.upload = upload;
exports.show = show;Index.js
var server = require("./server");
var router = require("./router");
var requestHandlers = require("./requestHandlers");
var handle = {}
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;
handle["/show"] = requestHandlers.show;
server.start(router.route, handle);有点难住了,感谢你的帮助。
发布于 2012-09-17 13:51:46
"/tmp/test.jpg"不是正确的路径-此路径以根目录/开头。
在unix中,指向当前目录的快捷方式是.
尝试此"./tmp/test.jpg"
发布于 2015-12-29 18:04:11
稍微扩展一下错误发生的原因:路径开头的正斜杠表示“从文件系统的根开始,并查找给定的路径”。没有正斜杠意味着“从当前工作目录开始,并查找给定的路径”。
路径
/tmp/test.jpg因此转换为在文件系统根目录下的临时文件夹(例如,windows上的c:\,*nix上的/)中查找文件test.jpg,而不是在webapp文件夹中。添加句点(.)在路径前面显式地将其更改为“从当前工作目录开始”,但基本上与完全去掉正斜杠相同。
./tmp/test.jpg = tmp/test.jpg发布于 2014-08-31 01:45:35
如果您的临时文件夹相对于运行代码的目录,请删除/tmp前面的/。
所以你只需要在你的代码中使用tmp/test.jpg。这对我来说在类似的情况下也是有效的。
https://stackoverflow.com/questions/9047094
复制相似问题