Meteor: Trying to serve a static file outside of /public

后端 未结 2 1230
忘了有多久
忘了有多久 2021-01-15 06:32

TLDR; How can one serve a static file through iron router? If I place a file in /public it bypasses all the hooks I could use in iron router.

Long version: I need

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-15 07:08

    You can use fs to server any file:

    Router.route('/static/:filename', function (){
      var fs = Npm.require('fs'),
          path = '/tmp/' + this.params.filename; // make sure to validate input here
    
      // read the file
      var chunk = fs.createReadStream(path);
    
      // prepare HTTP headers
      var headers = {}, // add Content-type, Content-Lenght etc. if you need
          statusCode = 200; // everything is OK, also could be 404, 500 etc.
    
      // out content of the file to the output stream
      this.response.writeHead(statusCode, headers);
      chunk.pipe(this.response);
    });
    

提交回复
热议问题