Meteor front-end RESTful API downloads files through the back-end API, meteorrestful
Meteor File Download Problems
The backend HTTP server provides a download interface, but the front-end Meteor must be able to open a URL for the browser user to download the file.
For example, an online Meteor Logo file is like a RESTful API provided by the backend. Then, we expose a URL to the browser client to download
Meteor dependency
Install all dependencies:
meteor add httpmeteor add cfs:http-methodsmeteor add froatsnook:request
Note:
* Cfs: http-methods *It is very convenient to create RESTful APIs for Meteor projects.
Here for details.
* Froatsnook: request *It is also very simple and convenient to provide a RESTful API for the Meteor project to facilitate access to binary data, and supports synchronous requests.
Here for details.
Sample Code Meteor Server
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
Check whether there is a Logo in the browser
meteor --port 3000
End
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.