Search code examples
node.jsexpressmiddleware

Request left hanging in express middleware despite next call


I have written simple express middleware to parse files and add them to req. It looks like this. import formidable from 'formidable';

export const parseForm = (req, res, next) => {
    const form = new formidable.IncomingForm();
    form.parse(req, (err, fields, files) => {
        if(err) {
            next(err);
        }
        req.files = files; // eslint-disable-line
        next();
    });
};

I'm adding it to app like this(along with other middleware).

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cors());
app.use(parseForm);
// static paths
const publicPath = express.static(path.join(__dirname, '../client/public'));
app.use(publicPath);

// routes
app.use('/api', adminRoutes);
app.use('/api', ingredientRoutes);
app.use('/api', productRoutes);

My problem is that despite calling next function in middleware my request is left hanging. Is next supposed to pass some arguments or maybe I'm passing my middleware in wrong fashion?

Thanks for help.


Solution

  • Turns out you can't use body-parser and formidable simultaneously. I resigned from body-parser and added to my middleware

    req.body = fields;