繁体   English   中英

如何在 npm 安装期间下载 static 文件

[英]How to download a static file during npm install

我的 angular 应用程序中有一个链接,单击该链接应打开 pdf 文件。 但是,应首先下载此文件并使其静态可用。

有没有办法在运行 npm 安装时通过 http 请求下载文件?

基本上,在托管应用程序之前,我需要下载文件并将其复制到 static 位置,然后从应用程序中的 href 引用该位置

就像是

npm download http://download.com/file.pdf

或者

instruct npm to download via paclage.json

您可以在应用程序的postinstall中定义安装后脚本:

package.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);
});

它将在您安装应用程序时执行(安装完所有内容后)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM