在Express.js简单APP中使用fs.unlink的ENOENT ERROR?

时间:2015-03-11 01:18:52

标签: javascript json node.js express filesystems

js.And我在express.js中创建了一个简单的博客。 我使用Typicode/lowdb as database来创建/更新/删除基于data.json文件上的id的帖子。并使用json数组中的slug数据创建/更新/删除带有节点的markdown帖子.js文件系统API 。在降价帖子的删除方上,我收到了一条我不明白的错误。

  { [Error: ENOENT, unlink 'C:\Users\path\to\publicfolder\undefined.md']
  errno: -4058,
  code: 'ENOENT',
  path: 'C:\\Users\\path\\to\\publicfolder\\undefined.md' }

我使用 fs.unlink 这样删除降价帖子:

   var oldSlug = "public/"+req.params.slug+".md";   

fs.unlink(oldSlug, function(err) {
    if(err) {
      console.log(err);
    } else {
      console.log("Deleted the old markdown file: " + oldSlug +"");
    }
  });

以下是部分公开的代码示例:

创建/更新/删除帖子的示例 - Post.js

       exports.index = function (req, res) {
        res.render('post/index', { posts: db.list()});
    }; 

    exports.form = function (req, res) {
            res.render('post/form');
        };

        exports.edit = function (req, res) {
        var post = db.getById(req.params.id);
        res.render('post/edit', {post: post})
        }

       exports.update = function (req, res) {

        var slugTitle = slug(req.body.name).toLowerCase().split('.').join("");

        var description = req.body.desc;

    db.update({id: req.body.id, name: req.body.name, desc: req.body.desc,
    slug: slugTitle});

//using this to rename and update markdown posts it work's

        var oldPath = "public/"+req.body.slug+".md";
         var newPath = "public/"+slugTitle+".md"; 

         fs.unlink(oldPath, function(err) {
            if(err) {
              console.log(err);
            } else {
              console.log("Deleted the old markdown file : oldPath ");
            }
          }); 
        fs.writeFile(newPath, description, function(err) {
                    if(err) {
                      console.log(err);
                    } else {
                      console.log("The post was updated as: newPath ");
                    }
                  }); 
        res.redirect('/post');};

        exports.remove = function (req, res) {
            db.remove(req.params.id);

//using this to delete markdown posts i get the error
//changing req.params.slug to req.body.slug still gives the error 

          var oldSlug = "public/"+req.params.slug+".md"; 
            fs.unlink(oldSlug, function(err) {
            if(err) {
              console.log(err);
            } else {
              console.log("Deleted the markdown file: " + oldSlug +"");
            }
          });
        res.redirect('/post');};

Server.js上的示例

app.get('/post', post.index);
app.get('/post/create', post.form);
app.post('/post/create', post.save);
app.get('/post/details/:id', post.details);
app.get('/post/edit/:id', post.edit);
app.post('/post/update', post.update);
app.get('/post/delete/:id', post.remove);
 http.createServer(app).listen(app.get('port'), function(){
    console.log("Express server listening on port " + app.get('port'));
 });

index.jade示例

doctype
html
    head
        title
    body
        h1 Posts
        ul
        each item in posts
            li Item #{item.name} - #{item.desc} - #{item.slug} 
                a(href='/post/details/#{item.id}') Details -
                a(href='/post/edit/#{item.id}') Edit -
                a(href='/post/delete/#{item.id}') Delete
        div
            p
                a(href='post/create') Add new

问题在于post.js文件中的exports.remove函数,但为什么?我相信我正在识别要删除的markdown文件,该文件对应于数据库中文档中的Slug参数或不是吗?我希望我能在代码中理解。

1 个答案:

答案 0 :(得分:0)

问题是您没有将变量slug传递到remove路线的任何位置。由于您使用的是GET,因此它不能来自req.body,因此您必须将其传递给URL路径或作为查询字符串。

尝试更改这些行:

post.js

var oldSlug = "public/"+req.query.slug+".md";

index.jade

a(href='/post/delete/#{item.id}?slug=#{item.slug}') Delete