简体   繁体   中英

How to get upload images in a other folder? express static issue

I currently have 2 folders, one named project that contain a public folder and a server folder. I am currently using a post method for uploading files (images). These are my 2 folders path:
project/router/index.js and project/client/public/uploads

I have use app.use(express.static('public')); so my project/router could have access to my public folder. The problem is I don't really know how to tell my function the path to use. When I do await fs.writeFile(`http://localhost:8080/uploads/${request.files[0].originalname}`, data); My router use the port 3000 and my client folder use the port 8080. At the end my function use the wrong path:

server\http:\localhost:8080\uploads\IMG_20200813_0006.jpg' and I want to use this one instead project/client/public/uploads

Thank you

await fs.writeFile('http...') is a double fail:)

if you want to use await you need to use promises: await fs.promises.writeFile()

you can't write to http, it makes no sens. For this you should make a proper request using node's http module or a simpler one, axios for exemple.

axios({
  method: 'post',
  url: '/user/12345',
  data: {
    firstName: 'Fred',
    lastName: 'Flintstone'
  }
});

why not use directly that?

await fs.promises.writeFile(`project/client/public/uploads/${request.files[0].originalname}`, data)

Finaly I fix my problem by changing my path for the uploads. I put it in my server/public folder and to get the images I have use v-bind:src="'http://localhost:3000/uploads/' + publication.image">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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