Home > Net >  Illegal operation on a directory error on Express JS
Illegal operation on a directory error on Express JS

Time:01-02

I am building a small image upload application using node 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.

 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. 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. I have pre-existing folder by name 'img'. I am not able to figure out, where is issue is popping.

Can anyone please help me out?

CodePudding user response:

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!

  • Related