【发布时间】:2013-09-03 20:47:05
【问题描述】:
我正在使用 XHR 从客户端向服务器端发送文件:
$(document).on('drop', function(dropEvent) {
dropEvent.preventDefault();
_.each(dropEvent.originalEvent.dataTransfer.files, function(file) {
// ...
xhr.open('POST', Router.routes['upload'].path(), true);
xhr.send(file);
});
})
现在我想响应这个 POST 服务器端并将文件保存到磁盘。 The docs 似乎只谈论在客户端处理事情;我什至不知道如何在服务器端上钩。
我现在所拥有的路线是这样的:
Router.map(function() {
this.route('home', {
path: '/'
});
this.route('upload', {
path: '/upload',
action: function() {
console.log('I never fire');
}
});
});
有了connect,我可以做到:
Connect.middleware.router(function(route) {
route.post('/upload', function(req, res) {
// my server-side code here
});
});
Iron-Router 有类似的吗?
深入研究内部,我发现 Meteor 在后台使用 connect,我可以这样做:
WebApp.connectHandlers.use(function(req, res, next) {
if(req.method === 'POST' && req.url === '/upload') {
res.writeHead(200);
res.end();
} else next();
});
但我不知道如何在这种情况下获得用户。
【问题讨论】:
-
刚刚注意到this comment。也许它甚至没有实现.....那么铁路由器有什么替代品?
标签: meteor iron-router