2012-01-28 39 views
75

tôi sau cùng với: The Node Beginner BookNode JS Lỗi: ENOENT

Sau khi thử nghiệm với mã từ một SO bài:

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); 
    } 
} 

Các lỗi tồn tại:

Error: ENOENT, no such file or directory 'tmp'

app dir structure

Quyền trên tmp là 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); 

Một chút bối rối, bất kỳ sự giúp đỡ đánh giá cao.

+1

'process.cwd()' của bạn là gì? – fent

+3

tìm thấy giải pháp ở đây: http://stackoverflow.com/questions/7681407/node-js-fs-stat-throws-enoent-the-operation-completed-successfully. Yêu thích diễn đàn này! – Wasabi

Trả lời

81

"/tmp/test.jpg" không phải là đường dẫn chính xác - đường dẫn này bắt đầu bằng / là thư mục gốc.

Trong unix, các shortcut vào thư mục hiện là .

Hãy thử điều này "./tmp/test.jpg"

+5

Ông giải thích nó chính xác. '/ tmp' khác với'./tmp'. '/ tmp' không nằm trong thư mục hiện tại, nó nằm trong thư mục gốc. – 3ocene

-2

sử dụng "tạm thời" thay cho "tmp"

"/temp/test.png"

nó hoạt động cho tôi sau khi tôi nhận ra tmp là một thư mục tạm thời không tồn tại trên máy tính của tôi, nhưng temp của tôi là thư mục tạm thời của tôi

///

EDIT:

Tôi cũng tạo ra một thư mục mới "tmp" trong C của tôi: lái xe và tất cả mọi thứ đã làm việc một cách hoàn hảo. Cuốn sách này có thể đã bỏ lỡ nhắc đến rằng bước nhỏ

séc ra http://webchat.freenode.net/?channels=node.js để trò chuyện với một số các cộng đồng Node.js

0

Bạn có thể bao gồm một tập tin ngọc khác nhau vào mẫu của bạn, mà đến từ một thư mục khác nhau

views/ 
    layout.jade 
static/ 
    page.jade 

để đưa file bố trí từ quan điểm dir để tĩnh/page.jade

page.jade

extends ../views/layout 
8

nếu thư mục tmp của bạn có liên quan đến thư mục nơi mã của bạn đang chạy xóa / trước /tmp.

Vì vậy, bạn chỉ có tmp/test.jpg trong mã của mình. Điều này làm việc cho tôi trong một tình huống tương tự.

18

Để mở rộng một chút về lý do lỗi xảy ra: Dấu gạch chéo ở đầu đường dẫn có nghĩa là "bắt đầu từ gốc của hệ thống tệp và tìm đường dẫn đã cho". Không có dấu gạch chéo chuyển tiếp có nghĩa là "bắt đầu từ thư mục làm việc hiện tại và tìm đường dẫn đã cho".

Đường dẫn

/tmp/test.jpg 

do đó dịch để tìm kiếm các tập tin test.jpg trong thư mục tmp tại thư mục gốc của hệ thống tập tin (ví dụ c: \ trên cửa sổ,/trên * nix), thay vì của thư mục web webapp. Thêm một dấu chấm (.) Ở phía trước đường dẫn một cách rõ ràng thay đổi điều này để đọc "bắt đầu từ thư mục làm việc hiện tại", nhưng về cơ bản là giống như rời bỏ dấu gạch chéo về phía trước hoàn toàn.

./tmp/test.jpg = tmp/test.jpg 
Các vấn đề liên quan