I want to find a file in a specific directory by it's name and without knowing it's extension
app.get("/test/:id", (req, res) => {
const mongo_id = req.params.id;
const file_path = __dirname + somepath + mongo_id + ".png"; // I should remove this extension but how to find file ?
if (!existsSync(file_path)) return res.status(404).end() ; // Check if file exists
res.sendFile(file_path) ;
})
As you see I am adding .png
at the end of the path that I want to send it as a response but sometimes I want to save some files that they are not PNG file
Thank you
I solved this by saving file extension during saving file information into the database
And when I send request for getting image response I get the file extension and necessary information for finding it in the goal directory and send the file into the response. Here's my code:
app.get("test/:id", (req, res) => {
const mongo_id = req.params.id;
MyModel.findById(mongo_id, (err, file) => {
if (err || !file) return res.status(404).end()
const file_path = __dirname + somepath + mongo_id + '.' + file.extension;
if (!existsSync(file_path)) return res.status(404).end();
res.sendFile(file_path);
})
})
But if anyone knows a solution that I mentioned in my question I would also appreciate it if they shared it.
I want to find a file in a specific directory by it's name and without knowing it's extension
app.get("/test/:id", (req, res) => {
const mongo_id = req.params.id;
const file_path = __dirname + somepath + mongo_id + ".png"; // I should remove this extension but how to find file ?
if (!existsSync(file_path)) return res.status(404).end() ; // Check if file exists
res.sendFile(file_path) ;
})
As you see I am adding .png
at the end of the path that I want to send it as a response but sometimes I want to save some files that they are not PNG file
Thank you
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.