我使用 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');
}
});
});
With connect, 我可以做:
Connect.middleware.router(function(route) {
route.post('/upload', function(req, res) {
// my server-side code here
});
});
有没有类似的东西铁路由器?
深入研究内部结构,我发现 Meteor 正在使用connect
在幕后,我可以做这样的事情:
WebApp.connectHandlers.use(function(req, res, next) {
if(req.method === 'POST' && req.url === '/upload') {
res.writeHead(200);
res.end();
} else next();
});
但我不知道如何在这种情况下吸引用户。