[Node.js] Trigger a File Download in Express
2023-09-14 09:00:49 时间
Downloading and saving a file is a common scenario when building out your web application. Using Express, you can either trigger a download from an existing file or set the headers on the response to send a file you created through Node. This lesson walks you through both approaches.
If the file is already exists on the server:
const express = require('express') const app = express() app.get('/', (req, res) => { res.download('./test.txt'); })
If files is not there:
app.get("/", (req, res) => { res.setHeader( "Content-disposition", "attachment; filename=message.json" ) res.setHeader("Content-type", "application/json") res.send(JSON.stringify({message: "Hello"})) })
相关文章
- js调用Webservice接口案例
- Node.js进程管理之子进程
- [Node.js] Reading from the File System in Node.js
- [Node.js] Resolve path problem in all envs (require.resolve)
- [Node.js] Sequelize Intro - 1
- [Tools] Create a Simple CLI Tool in Node.js with CAC
- [Node.js] Build microservices in Node.js with micro
- [Node.js] Use "prestart" in scripts
- [Node.js] Handle Relative Paths in a Node.js ESM Project with import.meta.url
- [Node.js] Resolve path problem in all envs (require.resolve)
- [Docker] Hooking a Volume to Node.js Source Code
- [Node.js] Stream all things!
- [Node.js] Show More Lines in a Node.js Error Stack Trace
- [Node.js] Read a File in Node.js with fs.readFile and fs.readFileSync
- [Node.js] Level 6. Socket.io
- [Node.js]30. Level 6: Listen 'Question' from client, and then Answer the Question
- Node.js 命令行程序开发教程
- Atitit 软件运行环境平台的变迁 attilax大总结 1.1.Native os时代 1.2.Vm时代 java net php 1.3.Script时代 js node。js 1.4.B
- JS 查看标签下某元素所有信息(查看元素,查询元素,遍历元素、HTMLCollection对象、NodeList对象)
- Node.js express模块(一)
- Node.js学习笔记——fs模块