generating and serving static files with Meteor

后端 未结 4 1584
温柔的废话
温柔的废话 2020-12-02 16:23

I\'m looking to create static text files based upon the content of a supplied object, which can then be downloaded by the user. Here\'s what I was planning on doing:

4条回答
  •  悲&欢浪女
    2020-12-02 16:39

    Another option is to use a server side route to generate the content and send it to the user's browser for download. For example, the following will look up a user by ID and return it as JSON. The end user is prompted to save the response to a file with the name specified in the Content-Disposition header. Other headers, such as Expires, could be added to the response as well. If the user does not exist, a 404 is returned.

    Router.route("userJson", {
        where: "server",
    
        path: "/user-json/:userId",
    
        action: function() {
            var user = Meteor.users.findOne({ _id: this.params.userId });
    
            if (!user) {
                this.response.writeHead(404);
                this.response.end("User not found");
                return;
            }
    
            this.response.writeHead(200, {
                "Content-Type": "application/json",
                "Content-Disposition": "attachment; filename=user-" + user._id + ".json"
            });
            this.response.end(JSON.stringify(user));
        }
    });
    

    This method has one big downside, however. Server side routes do not provide an easy way to get the currently logged in user. See this issue on GitHub.

提交回复
热议问题