expressjsHow do I delete a file using Express.js?
To delete a file using Express.js, you can use the fs.unlink() method. This method takes the path of the file to be deleted as its argument. The code below shows an example of how to use it:
// Import the filesystem module
const fs = require('fs');
// Path of the file to be deleted
const filePath = '/path/to/file.txt';
// Delete the file
fs.unlink(filePath, (err) => {
if (err) throw err;
console.log(`${filePath} was deleted`);
});
Output example
/path/to/file.txt was deleted
The code above consists of the following parts:
const fs = require('fs')imports the filesystem module.const filePath = '/path/to/file.txt'specifies the path of the file to be deleted.fs.unlink(filePath, (err) => {...})calls thefs.unlink()method, which takes the path of the file to be deleted as its argument.if (err) throw errchecks if an error occurred and throws it if it did.console.log(${filePath} was deleted)logs a message to the console indicating that the file was deleted.
For more information on deleting files using Express.js, see the following links:
More of Expressjs
- How can I disable the X-Powered-By header in Express.js?
- How do I use Express.js to parse YAML files?
- How can I set up X-Frame-Options in ExpressJS?
- How do I use Yarn to add Express.js to my project?
- How can I use the x-forwarded-for header in Express.js?
- How can I use Express.js to generate a zip response?
- How can I use Express.js with React to develop a web application?
- How can I use an ExpressJS webhook to receive data from an external source?
- How do I create an Express.js boilerplate?
- What is Express.js and how is it used for software development?
See more codes...