Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
102 views
in Technique[技术] by (71.8m points)

javascript - How to download a static file during npm install

I have a link in my angular app which upon clicking should open a pdf file. But, this file should first be downloaded and to make it statically available.

Is there a way to download a file via http request while running npm install?

Basically, before hosting the app I need to download and copy a file to a static location which is then referred to from the href within the app

Something like

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

or

instruct npm to download via paclage.json
question from:https://stackoverflow.com/questions/65601289/how-to-download-a-static-file-during-npm-install

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can define a postinstall script in your app's package.json :

package.json

{
  "name": "my-app",
  "version": "1.0.0",
  "scripts": {
    "start": "node src/index.js",
    "postinstall": "node downloadAssets.js", // <--------------------------
    // ...
  },
  "dependencies": { /* ... */ }
  // ...
}

And then create a script to do it:

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

It will be executed when you install your app (after everything is installed)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...