简体   繁体   English

Express JS上目录错误的非法操作

[英]Illegal operation on a directory error on Express JS

I am building a small image upload application using node js.我正在使用节点 js 构建一个小型图像上传应用程序。 I have imported proper packages, have run the methods as it has to be.我已经导入了正确的包,已经运行了必须的方法。 Image is being retrieved as multipart-form data.图像被检索为多部分形式的数据。 But inside the mv() function I am getting the following error.但是在 mv() function 里面我得到了以下错误。

 node:internal/process/promises:246
              triggerUncaughtException(err, true /* fromPromise */);
              ^
    
    [Error: EISDIR: illegal operation on a directory, open 'E:\Workspace\clean_blog_10\public\img'] {
      errno: -4068,
      code: 'EISDIR',
      syscall: 'open',
      path: 'E:\\Workspace\\clean_blog_10\\public\\img' 

I have imported 'express-fileupload' , 'path' & invoked the methods correctly.我已经导入了'express-fileupload''path'并正确调用了这些方法。 The issue is arising from the last request handler mentioned.该问题是由提到的最后一个请求处理程序引起的。

const express = require('express');
const path = require('path');
const ejs = require('ejs');
const mongoose = require('mongoose'); //importing mongoose
const BlogPost = require('./models/BlogPost');
const fileUpload = require('express-fileupload');
const {resourceUsage} = require('process');

mongoose.connect('mongodb://localhost/my_database', { useNewUrlParser: true }) //Defining a connection


const app = new express();
app.set('view engine', 'ejs')

app.use(express.static('public'));  
app.use(express.json());
app.use(express.urlencoded());
app.use(fileUpload());

app.listen(4000, () => {
    console.log('App listening on port 4000');
});

app.get('/', async (req, res) => {

    const blogposts = await BlogPost.find({})
    //console.log(blogposts) 
    res.render('index', {
        blogposts: blogposts 
    });
});



app.get('/posts/new', (req, res) => {
    res.render('createPost');
});

/*The below request handler is having an issue*/

app.post('/posts/store', async (req, res) => {

    let image = req.files.image

    image.mv(path.resolve(__dirname, 'public/img'), image.name,
        async (error) => {
            await BlogPost.create(req.body)
            res.redirect('/')
        })

});

My EJS file has a form which is retrieving multimedia form data in a correct way & I have verified.我的 EJS 文件有一个表单,它以正确的方式检索多媒体表单数据并且我已经验证。 I have pre-existing folder by name 'img' .我有一个名为'img' 的预先存在的文件夹。 I am not able to figure out, where is issue is popping.我无法弄清楚,问题出在哪里。

Can anyone please help me out?谁能帮帮我?

Sorry, it was a typo-error... I had not closed the path.resolve() parentheses at the right place, The method has three arguments, but I had closed at the second argument!抱歉,这是一个拼写错误...我没有在正确的位置关闭 path.resolve() 括号,该方法有三个参数,但我在第二个参数处关闭了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM