如何解决'错误:ENOENT:没有这样的文件或目录'在Nodejs中

时间:2018-05-09 11:01:43

标签: node.js

我想解决Nodejs中的错误   我有以下代码 -

var http = require('http');
var fs = require('fs');
var url = require('url');
var path = require('path');

function ensureDirectoryExistence(pathname) {
    var dirname = path.dirname(pathname);
    if (fs.existsSync(dirname)) {
      return true;
    }
    ensureDirectoryExistence(dirname);
    fs.mkdirSync(dirname);
  }

http.createServer( function (request, response) {  
   var pathname = url.parse(request.url).pathname;
   console.log(pathname);

    console.log("Request for " + pathname + " received.");

   fs.readFile(pathname.substr(1), function (err, data) {
      if (err) {
         console.log(err);
         response.writeHead(404, {'Content-Type': 'text/html'});
      }else {   
         response.writeHead(200, {'Content-Type': 'text/html'});    

         response.write(data.toString());       
      }
      response.end();
   });   
}).listen(8081);

console.log('Server running at http://127.0.0.1:8081/');

我收到错误

  

http://127.0.0.1:8081/运行的服务器       POW!       将/index.htm       收到/index.htm的请求。       {错误:ENOENT:没有这样的文件或目录,打开       ' C:\用户\ Ryuzaki \桌面\实习生\沃森\ index.htm的'         错误:-4058,         代码:' ENOENT',         系统调用:'打开',         路径:' C:\ Users \ Ryuzaki \ Desktop \ Intern \ watson \ index.htm' }

请告诉我如何解决此错误? 我无法创建路径或目录。

0 个答案:

没有答案