节点JS错误:ENOENT [英] Node JS Error: ENOENT

查看:108
本文介绍了节点JS错误:ENOENT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跟着: 节点初学者书

使用另一篇SO帖子中的代码进行测试后:

After testing with the code from another SO post:

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'

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

tmp上的权限为777.

The permissions on tmp are 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);

有些困惑,任何帮助表示赞赏.

A little stumped, any help appreciated.

推荐答案

"/tmp/test.jpg"不是正确的路径-此路径以/开头,它是根目录.

"/tmp/test.jpg" is not the correct path – this path starts with / which is the root directory.

在UNIX中,当前目录的快捷方式是.

In unix, the shortcut to the current directory is .

试试这个"./tmp/test.jpg"

这篇关于节点JS错误:ENOENT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆