javascript - 如何在 npm install 期间下载静态文件
问题描述
我的 Angular 应用程序中有一个链接,单击该链接应打开一个 pdf 文件。但是,应首先下载此文件并使其静态可用。
有没有办法在运行 npm install 时通过 http 请求下载文件?
基本上,在托管应用程序之前,我需要下载文件并将其复制到静态位置,然后从应用程序中的 href 引用该位置
就像是
npm download http://download.com/file.pdf
或者
instruct npm to download via paclage.json
解决方案
postinstall
您可以在应用程序的 package.json中定义一个 脚本:
包.json
{
"name": "my-app",
"version": "1.0.0",
"scripts": {
"start": "node src/index.js",
"postinstall": "node downloadAssets.js", // <--------------------------
// ...
},
"dependencies": { /* ... */ }
// ...
}
然后创建一个脚本来执行此操作:
下载Assets.js
const http = require('http');
const fs = require('fs');
const file = fs.createWriteStream("file.pdf");
http.get("http://download.com/file.pdf", function(response) {
response.pipe(file);
});
它将在您安装应用程序时执行(安装完所有内容后)
推荐阅读
- java - 我必须同时放置 persistence.xml 和 hibernate.cfg.xml 吗?我部署时似乎有冲突
- javascript - 如何在 React 中通过单击动态添加的按钮触发功能?
- ios - 检测用户是使用粘贴板(一次性密码)还是手动操作
- sql - Liquibase Gradle 插件 2.0.2 回滚任务报错
- java - 避免黄瓜中的例子
- c# - 如何在 EF Core 中仅包含相关表的最新记录
- javascript - 使用 java 脚本创建的锚标记调用指向成功或失败的 API 调用
- java - 如何将函数插入目录/哈希表
- java - 尝试将行添加到 DefaultTableModel 模型时出现 Java 空指针异常
- r - R - 计划比较中完全关闭的自由度(使用 afex - aov_ez - emmeans - 对)?