我正在使用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);
});
})
Run Code Online (Sandbox Code Playgroud)
现在我想响应这个POST服务器端并将文件保存到磁盘.文档似乎只谈论处理客户端的事情; 我甚至不知道如何在服务器端获得一个钩子.
我现在所有的路线都是这样的:
Router.map(function() {
this.route('home', {
path: '/'
});
this.route('upload', {
path: '/upload',
action: function() {
console.log('I never fire');
}
});
});
Run Code Online (Sandbox Code Playgroud)
通过连接,我可以做到:
Connect.middleware.router(function(route) {
route.post('/upload', function(req, res) {
// my server-side code here
});
});
Run Code Online (Sandbox Code Playgroud)
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();
}); …Run Code Online (Sandbox Code Playgroud) 当url是/ company/martin时,以下代码不起作用:
this.route('scheduledTasks', {
path: '/company/:name',
data: {
items:
function(){
return scheduledTasks.find({company: this.params.name});
}
}
Run Code Online (Sandbox Code Playgroud)
虽然这确实有效:
this.route('scheduledTasks', {
path: '/company/:name',
data: {
items:
function(){
return scheduledTasks.find({company: "martin"});
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试了几个解决方案,但似乎this.params.name不存在或无法访问?
在meteor.com和heroku上部署Meteor应用程序时遇到与路由有关的奇怪"错误".当应用程序在本地运行时,一切正常.然而,当部署应用程序时 - 我已经在meteor.com的简单主机和Heroku上尝试了这一点 - 我得到了默认的Iron Router设置页面,其中包含有关如何设置我的第一条路线的说明,即使路线已定义正确并在本地工作(下面的截图).这发生在应用程序的所有页面上.
以下是该页面的屏幕截图:http://i.stack.imgur.com/GlNd9.png
我有在服务器端发布的集合,并使用铁路由器的waitOn订阅这些集合.但是,在客户端,我永远看不到对服务器端定义的集合的任何引用.我可以访问它们的唯一方法是在客户端定义集合(devices = new Meteor.Collection('devices')),但我没有看到有人在他们的在线示例中这样做.这是代码:
客户代码:
Router.route('/devices', {
waitOn: function() {
return [
Meteor.subscribe('devices', Meteor.user()._id),
];
},
action: function() {
this.render();
}
});
Run Code Online (Sandbox Code Playgroud)
服务器端:
Devices = new Mongo.Collection("devices");
Devices.allow({
'insert': function (userId, doc) {
if (userId === doc.accountId) {
return true;
}
return false;
},
'update': function (userId, doc) {
if (userId === doc.accountId) {
return true;
}
return false;
},
});
Meteor.publish('devices', function(id) {
return Devices.find({accountId: id});
});
Run Code Online (Sandbox Code Playgroud)
我已经删除了自动发布,并且从在线示例中,我应该能够引用Devices.find({}).相反,我必须使用devices = new Meteor.Collection('devices')然后引起问题,因为如果我再次调用它,它会说我已经有一个名为devices的集合.有谁知道为什么我不能参考设备?
我已经尝试使用Iron Router进行服务器路由,但这不起作用.然后我发现了WebApp,它似乎应该处理这个问题.
但是当我检查req对象时:
WebApp.connectHandlers.use("/api/add", function( req, res, next ) {
console.log( req );
res.writeHead(200);
res.end("Hello world from: " + Meteor.release);
});
Run Code Online (Sandbox Code Playgroud)
我没有看到任何POST表单数据.没有身体属性,我没有看到任何其他属性下的数据本身.
我该如何访问这些数据?我疯狂地想弄清楚我认为会比较简单的东西......