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:

  1. const express = require('express')
  2. const app = express()
  3.  
  4. app.get('/', (req, res) => {
  5. res.download('./test.txt');
  6. })

If files is not there:

  1. app.get("/", (req, res) => {
  2. res.setHeader(
  3. "Content-disposition",
  4. "attachment; filename=message.json"
  5. )
  6. res.setHeader("Content-type", "application/json")
  7. res.send(JSON.stringify({message: "Hello"}))
  8. })

[Node.js] Trigger a File Download in Express的更多相关文章

  1. node.js delete directory & file system

    node.js delete directory & file system delete a not empty directory https://nodejs.org/api/fs.ht ...

  2. 使用node.js生成excel报表下载(excel-export express篇)

    引言:日常工作中已经有许多应用功能块使用了nodejs作为web服务器,而生成报表下载也是我们在传统应用. java中提供了2套类库实现(jxl 和POI),.NET 作为微软的亲儿子更加不用说,各种 ...

  3. 2015年12月12 Node.js实战(一)使用Express+MongoDB搭建多人博客

    序,Node是基于V8引擎的服务器端脚本语言. 基础准备 Node.js: Express:本文用的是3.21.2版本,目前最新版本为4.13.3,Express4和Express3还是有较大区别,可 ...

  4. Node.js 蚕食计划(三)—— Express 启航

    如果看过上一篇<Node.js 蚕食计划>,就会发现手动搭建一个 web 服务器还是比较繁琐 而 express 就是一个可以极大地提高开发效率的 web 开发框架 一.创建项目 在 ex ...

  5. [Node.js] Read a File in Node.js with fs.readFile and fs.readFileSync

    We'll read a csv file in node.js both synchronously, and asynchronously. The file we're reading is a ...

  6. Node.js 蚕食计划(四)—— Express + SQL Server 搭建电影网站

    前段时间在慕课网上看了 scott 大神的<node+mongodb建站攻略>课程,按照自己的思路做了一遍,发博客记录一下 一.项目介绍 这个项目是一个简单的电影网站,由首页.详情页.评论 ...

  7. 【Node.js】一、搭建基于Express框架运行环境+更换HTML视图引擎

      1)安装express generator生成器 这个express generator生成器类似于vue-cli脚手架工具,用来创建一个后端项目,首先我们要对生成器进行全局安装,在cmd中输入下 ...

  8. node.js入门学习(六)--express

    1.官网:http://expressjs.com/ 中文:http://www.expressjs.com.cn/ 2.HelloWorld 1)mkdir node-express-demo 2) ...

  9. Node.js在任意目录下使用express命令‘不是内部或外部命令’解决方法

    1.一开始我只能在nodejs全局目录下使用express命令建一个新的项目,建在其他任意一个目录命令行都会提示"不是内部或外部命令",导致目录会乱,目录如下. 2.尝试了一会,发 ...

随机推荐

  1. mbist summary

    1. 关于mbist,网上也有介绍,觉得不错: 推荐的mbistt的博客:奋斗的猪 2.使用的工具是mbistarchitect,不是tessent. 3.工具使用的相关文档:从EETOP和工具自带的 ...

  2. 查找闲置ip

    首先ping 一下该网段的广播地址,如ping 192.168.0.255 ,然后使用arp -a就可以查看到那些地址是被使用的,以及网卡的mac值.剩余的是暂时还未被使用的,当然没开机的机器就看不出 ...

  3. Mining of Massive Datasets-1

    given lots of data->discover patterns and models that are: valid, useful, unexpected, understanda ...

  4. LeetCode(112) Path Sum

    题目 Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  5. STM32L0开发——ADC多通道采集,IDE和IAR开发注意事项

    keil开发L0系列是免费的,官方提供许可的.因此建议Keil开发,L011F3由于flash只有8K,因此不建议HAL库,建议使用cubemx+LL(或snippets库).0.起初,可以参考官方库 ...

  6. Verilog学习笔记基本语法篇(四)·········块语句

    块语句是指将两条或者两条以上的语句组合在一起,使其在格式上更像一条语句.块语句分为两种: 1)用begin_end语句,通常用来标识顺序执行的语句,用它标识的块称作顺序块: 2)用fork_join语 ...

  7. PAT Basic 1056

    1056 组合数的和 给定 N 个非 0 的个位数字,用其中任意 2 个数字都可以组合成 1 个 2 位的数字.要求所有可能组合出来的 2 位数字的和.例如给定 2.5.8,则可以组合出:25.28. ...

  8. debian 中的jdk

    1 openjdk 2 debian安装java的脚本 3 下载bin自己编译

  9. c#中的String方法

    1.Replace(替换字符):public string Replace(char oldChar,char newChar);在对象中寻找oldChar,如果寻找到,就用newChar将oldCh ...

  10. 大数据学习——sqoop安装

    1上传  sqoop-1.4.6.bin__hadoop-2.0.4-alpha.tar.gz 2解压 .bin__hadoop--alpha.tar.gz 3重命名 .bin__hadoop--al ...