首先我这里是前端直接穿的图片url过来 也就是 https://192.168.100.33:8888/images/signImage/8178_0.png,我这里想要删除public下面的/images/signImage/8178_0.png,所以我这里需要先拆分出路径部分 然后和拼接出来你想要的路径
- const fs = require('fs')
- const url = require('url')
- router.post('/delete',async(req,res)=>{
- //imageUrl为https://192.168.100.33:8888/images/signImage/8178_0.png
- const { imageUrl } = req.body
- 拆分后的 delImage为/images/signImage/8178_0.png
- const delImage = url.parse(imageUrl).pathname //路径部分
- //删除文件
- fs.unlinkSync('./public'+delImage)
- })