Adding persistent storage to Mern Stack App

imraj560
TRIALOP

a year ago

I have a simple MERN CRUD Application where images are uploaded, deleted and edited. My images are getting uploaded fine in local environment, after setting up my app in railway, my app works fine but my images are not getting uploaded. I understand that it has something to do with persistent volume, i tried setting it up but it was a vain attempt. I am sure there are issues with my path. Can Someone help me regarding this matter.

Github Repo: https://github.com/imraj560/MERN-Exercise-Tracker

Local file upload path in backend:

const storage = multer.diskStorage({
    destination: function (req, file, cb) {
      cb(null, './uploads')
      // cb(null, __dirname +'/images')
    },
    filename: function (req, file, cb) {
      const uniqueSuffix = Date.now()
      cb(null, uniqueSuffix + file.originalname)
    }
  })

Route:

/POST a new workout
router.post('/', upload.single('file'), newWorkout);

Could you please advice what my path should be after after uploading to railway and setting up Volume for my project

30 Replies

a year ago

What is your current mount point for the volume?


imraj560
TRIALOP

a year ago

i put my mount path as /app/uploads


imraj560
TRIALOP

a year ago

filebrowser-production-46d4.up.railway.app, this is my file browser domain where i created an uploads folder, should i put this this domain link in my code eg: filebrowser-production-46d4.up.railway.app/uploads


a year ago

The filebrowser has nothing to do with your code, please attach a screenshot of your project canvas.


imraj560
TRIALOP

a year ago

I have attached a ss of project canvas, browser disk path and also the ss of the file browser settings


a year ago

Expand the group please.


imraj560
TRIALOP

a year ago

Not sure what you mean but there is a drop down option

Attachments


a year ago

The volume is not mounted to the correct location.Don't know why you are using filebrowser though.


imraj560
TRIALOP

a year ago

what should my location be


imraj560
TRIALOP

a year ago

the file gets uploaded in the uploads folder which is in the backend folder of my mern app in local environment


a year ago

You already answered that correctly earlier...

i put my mount path as /app/uploads

Yet you don't have it mounted to the path you said you do.


imraj560
TRIALOP

a year ago

But you can see from the ss the path is put as /uploads, am i missing somethng


a year ago

If I wasn't clear /app/uploads is the correct path, you do not have the volume mounted to that path.


imraj560
TRIALOP

a year ago

ok,

Attachments


a year ago

Correct.


imraj560
TRIALOP

a year ago

still my files are not getting uploaded on my app


a year ago

What makes you say that.


imraj560
TRIALOP

a year ago

this is my app where i upload images with text, my text gets saved but my images are still not getting uploaded. Hmm. am i using the wrong service for this issue

Attachments


a year ago

That's not a good indicator of whether or not images are being uploaded, as that could just indicate an issue with your code, going to need more information.


imraj560
TRIALOP

a year ago

Is there anything else i can provide


a year ago

How are you serving images?


imraj560
TRIALOP

a year ago

I am uploading images using multer image upload package to folder named upload in my backend which i have uploaded in Railway, my front-end built using react js is run from netlify


a year ago

I'm sorry but that does not answer the question.


imraj560
TRIALOP

a year ago

Below code is how i write my file to the upload folder in my backend

const storage = multer.diskStorage({
    destination: function (req, file, cb) {
      cb(null, './uploads')
      // cb(null, __dirname +'/images')
    },
    filename: function (req, file, cb) {
      const uniqueSuffix = Date.now()
      cb(null, uniqueSuffix + file.originalname)
    }
  })

a year ago

That does not answer the question, I asked how are you serving images that have been uploaded?


imraj560
TRIALOP

a year ago

I use a route which i created in the backend which serves the image. To retrieve those images in the front end, i put the URL related to the route in the image src to retrieve the image


a year ago

Please show that code.


imraj560
TRIALOP

a year ago

Route:

router.get('/download/:filename', downloadImage)

Controller:

const downloadImage = async(req, res) =>{


    const fileName = req.params.filename
    const filePath = path.join(__dirname, '../uploads', fileName)

    try{

        if(fs.existsSync(filePath)){

            res.sendFile(filePath)
           
        }else{

            res.status(404).send('File not found')
        }

    }catch(error){

        console.log(error)
    }

    
}

Front-end

 <Col md={4} className='p-1' style={{display:'flex', justifyContent:'center', alignItems:'center'}}>
            <Card key={props._id} style={{ width: '100%' }}>
            <Card.Img style={{height:'320px'}} variant="top" src={"https://mern-exercise-tracker-production.up.railway.app/api/workout/download/"+props.image} />
            <Card.Body>
            <Card.Title>{props.title}</Card.Title>
            <ListGroup className="list-group-flush">
                <ListGroup.Item>Reps: {props.reps}</ListGroup.Item>
                <ListGroup.Item>Weight: {props.load} kg</ListGroup.Item>
                <ListGroup.Item>Date: {props.createdAt}</ListGroup.Item>
            </ListGroup>
            </Card.Body>
            </Card>
            </Col>

a year ago

What status do you get from the backend when the frontend requests an image?


imraj560
TRIALOP

a year ago

Hi, my solution has been solved. As far as I have noticed, i think I was accessing the image from the server in a wrong way

The above code snippet is the correct way, now I am able to upload and display images properly using my app. Api code from github was not deployed properly so it was not working back then. Its working without using the volume service which is bit of a surprise because as far as i now you needed the volume service for uploading files. Is there something I am missing.

Attachments


Loading...
Adding persistent storage to Mern Stack App - Railway Help Station