0

I have a problem that I can't find a solution for a long time. It only happens when uploading a file that is already in the upload folder (file with same name). If I upload file for the first time it works fine. I'm using express-fileupload package.

I used this code as a reference https://github.com/bradtraversy/react_file_uploader

My form worked fine until I added file upload, first I thought that its something wrong on the front end, I use React and thought there is some state that is changing and causing the page to reload, but later I used Postman and it still reloads my app, doesn't matter on what page I am, even If I log out and upload the file with the Postman it reloads the page, but only if the file is already uploaded. This doesn't happen in the code above.

This is how my code looks

controller/upload.js

exports.upload_file = (req, res) => {

    if (req.files !== null) {

        const file = req.files.atest;
              
        file.mv(`${process.cwd()}/client/public/atesti/${file.name}`, err => {
            if (err) {
                return res.status(500).json({ ok: false, message: "Unos nije snimljen - greška kod atesta" });
            }

            res.status(200).json({ ok: true, message: "Atest je snimljen" });
        });
    } else return res.status(400).json({ ok: false, message: "Unos nije snimljen - fajl nije snimljen" });

}

routes/upload.js

const express = require('express');
const router = express.Router();
const uploadControlled = require('../controller/upload');
const routeAuth = require('../auth/auth');


router.post('/', routeAuth, uploadControlled.upload_file);


module.exports = router;

server.js

const express = require('express');
const cookieParser = require('cookie-parser');
const fileUpload = require('express-fileupload');

const app = express();
app.use(cookieParser());
app.use(fileUpload());

const PORT = process.env.PORT || 5000;

app.use(express.json());

//Stop CORS errors
app.use((req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Authorization');
    if (req.method === 'OPTIONS') {
        res.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH, DELETE, GET');
        return res.status(200).json({});
    }
    next();
});

const lagerListaRoute = require('./api/routes/items');
const korisniciRoute = require('./api/routes/users');
const refreshTokenRoute = require('./api/routes/refreshToken');
const seceneCeviRoute = require('./api/routes/seceneCevi');
const rezervacijeRoute = require('./api/routes/rezervacije');
const uploadRoute = require('./api/routes/upload');
const searchRoute = require('./api/routes/search');

app.use('/lagerlista', lagerListaRoute);
app.use('/users', korisniciRoute);
app.use('/refreshToken', refreshTokenRoute);
app.use('/seceneCevi', seceneCeviRoute);
app.use('/rezervacije', rezervacijeRoute);
app.use('/upload', uploadRoute);
app.use('/search', searchRoute);

app.use((req, res, next) => {
    const error = new Error('Not found');
    error.status = 404;
    next(error);
});

app.use((error, req, res, next) => {
    res.status(error.status || 500);
    res.json({
        error: {
            message: error.message
        }
    });
});

app.listen(PORT, () => {
    console.log(`Server is running on port ${PORT}`);
});  

I tried adding everything in server.js, like in the example code, without splitting it in the controller and route folder and without authentication, but it still reloaded the page.

If someone is wondering why am I uploading the same file more times, sometimes I need to change the uploaded file but leave the same name, and in my app file is connected to an item and sometimes the file is shared with few items so when I need to connect the same file with different item I use same file upload. When the file is uploading I use its location and save it in the database.

1 Answer 1

0

After few more days of bug searching, I found how to fix it. It looks like there is a problem with react-scripts, I had version 3.4.0 installed but when I downgraded it to 3.0.1 upload started working normally, also version 3.1.0 works fine. The upload works normally when I create a build folder, then the version of react-scripts doesn't matter.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.