後端 HTTP server提供一個下載接口,但是需要前端 Meteor 能夠給瀏覽器用戶開一個URL來下載這個文件。javascript
舉例:在線的Meteor Logo文件就比如後端提供的 RESTful API,而後咱們給瀏覽器客戶暴露一個 URL 來下載前端
安裝所有依賴:java
meteor add http meteor add cfs:http-methods meteor add froatsnook:request
說明:
* cfs:http-methods * 用來給Meteor項目提供一個方便建立 RESTful API 的機會。很方便。後端
here for details.瀏覽器
* froatsnook:request * 用來給Meteor項目提供一個便利訪問二進制數據的 RESTful API 的機會,也是很簡潔方便。支持同步請求。bash
here for details.markdown
if (Meteor.isServer) {
// exports a RESTful API for browser
HTTP.methods({
// name RESTful API as "GET /download-meteor-logo"
'/download-meteor-logo': function() {
// A file in streaming, so need to response to browser as a streaming.
var res = this.createWriteStream();
// Play as a HTTP client for requesting image.
// It is Sync way
var result = request.getSync("https://meteor.com/meteor-logo.png", {
encoding: null
});
var buffer = result.body;
// TODO: need to set header for response here which transfered by
// response of logo request.
res.write(buffer);
res.end();
}
});
} // Meteor.isServer enclosure
meteor --port 3000