expressjsHow do I send a file using Express.js?
To send a file using Express.js, you can use the sendFile method. This method takes in the path of the file you want to send as its first argument.
For example:
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
This will send the index.html file located in the same directory as the script.
The sendFile method also takes an optional options object as its second argument. This object can be used to set the root and dotfiles properties. The root property is used to set the root directory from which files are served, and the dotfiles property is used to specify if dotfiles should be served or not.
For example:
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html', {
root: '/home/user/files',
dotfiles: 'deny'
});
});
This will send the index.html file located in the /home/user/files directory, and will deny any requests for dotfiles.
Helpful links
More of Expressjs
- How can I use Node.js and Express together to create a web application?
- How do I use Express.js to upload a file to Amazon S3?
- How can I use Express.js to generate a zip response?
- How can I create a boilerplate project with Express.js and TypeScript?
- How can I use express-zip js to zip and download files?
- How do I set the time zone in Express.js?
- How do I set up a YAML configuration file for a Node.js Express application?
- How can I use Express.js and Winston together to create a logging system?
- How do I manage user roles in Express.js?
See more codes...