Search code examples
javascriptmeteormeteoriteiron-router

how can i convert this code using iron-router instead of router


https://github.com/davebryson/meteor_file_upload_example

the above is the original code using Meteor.router

and i wanted to convert this code only using iron-router, instead of previous router package

but the problem is when i upload file,

i can't understand how to convert these code using iron-router api.

i think there's a problem with index.html and serverside main.js code but i can't fix that.

would you please convert this code below using iron router plz?

in serverside main.js in the original code.

Meteor.Router.configure(
{
    bodyParser:
    {
        uploadDir: 'uploads',
        hash: 'sha1',
        keepExtensions : true
    }
}
);



Meteor.Router.add('/upload/file', 'POST',
 function()
 {
     var title = this.request.body.fileTitle,
     size = this.request.files.fileInfo.size,
     path = this.request.files.fileInfo.path,
     name = this.request.files.fileInfo.name,
     obj = {title: title, size: size, path: path, name: name};

     Files.insert(obj);
     // redirect to root
     return [301, {Location: '/'}, ""]
 }
 );

and i'v already converted the code in clientside main.js like below

Router.map(function () {
    this.route('listFiles', {path: '/'});
    this.route('addFile', {path: '/add'});
});


Template.listFiles.helpers({
  list: function () {
    return Files.find({});  
  }
});

Template.listFiles.events({
  'click #addFileBtn' : function(e) {
    e.preventDefault();
    Router.go('/add');
  } 
});

Template.addFile.events({

  'click #cancelBtn': function(e){
    e.preventDefault();
    Router.go('/');
  }

});

Meteor.subscribe("files");

the point is that i can't handle how to use http method in serverside code with iron-router


Solution

  • Meteor now directly allows you to attach middleware to the server using the WebApp package, with standard Connect handlers.

    Put webapp inside .meteor/packages of your app. You can then attach middleware to WebApp.connectHandlers inside your app. It's not too well documented yet, but treat it as a normal connect server.