Meteor front-end RESTful API downloads files through the back-end API, meteorrestful

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.