2014-03-19 17 views
0

我正在關注Discover Meteor書籍,並將這本書將js文件放在/ collections文件夾下。 JS文件定義了服務器端的方法:MeteorJS在/ server文件夾下放什麼?

Meteor.methods({ 
    post: function(postAttributes) { 
    var user = Meteor.user(); 
    var postWithSameLink = Posts.findOne({url: postAttributes.url}); 

    // ensure the user is logged in 
    if (!user) 
     throw new Meteor.Error(401, "You need to login to post new stories"); 

    // ensure the post has a title 
    if (!postAttributes.title) 
     throw new Meteor.Error(422, 'Please fill in a headline'); 

    // check that there are no previous posts with the same link 
    if (postAttributes.url && postWithSameLink) { 
     throw new Meteor.Error(302, 
     'This link has already been posted', 
     postWithSameLink._id); 
    } 

    // pick out the whitelisted keys 
    var post = _.extend(_.pick(postAttributes, 'url', 'title', 'message'), { 
     userId: user._id, 
     author: user.username, 
     submitted: new Date().getTime(), 
     commentsCount: 0, 
     upvoters: [], 
     votes: 0 
    }); 

    var postId = Posts.insert(post); 
    return postId; 
    }, 
}); 

那麼,在這種情況下,是不是因爲Meteor gathers all JavaScript files in your tree, with the exception of the server, public, and private subdirectories, for the client.公共訪問的整個邏輯是什麼?

這是一個問題嗎? 我應該把什麼server文件夾?

+0

'server /'文件夾中的任何代碼都不能使用延遲補償。在客戶端上定義的方法將作爲method * stubs *運行,這些客戶端模擬可以極大地提高應用程序的表觀速度,然後在服務器最終響應時被覆蓋。同樣,可以在客戶端和服務器上定義允許/拒絕規則等功能,以啓用延遲補償。因此,爲了獲得最佳性能,唯一應該放在'server /'中的代碼應該是不能或不應該(例如出於安全考慮)的代碼受延遲補償的影響。 – sbking

回答

0

有許多隻有服務器的事情可能該文件夾中去 - 訂閱,允許規則,cron作業等

server文件夾是從你的代碼是不向一般公衆可見的唯一地方。當然,這是您應該放置與安全相關的代碼的地方:允許/拒絕規則,帳戶配置等。如果您與外部API交談並且想要將代碼放入代碼中,/ server文件夾是唯一可以接受的地方這樣做。

將您的服務器端邏輯放在可訪問的文件夾中不是一個關鍵問題,因爲服務器端代碼不能從客戶端更改。這裏唯一的安全問題是,如果你在某處做了一個快捷方式,有人可能會研究你的代碼並找到一個後膛。此外,您使用客戶端不需要的代碼堵塞連接。

我也想說,大多數方法應該在/服務器,但這取決於你需要什麼。在客戶端上訪問代碼的方法允許利用延遲補償(here,示例下方的第三段),但是您需要確保自己的客戶端模擬不會產生干擾實際的副作用(服務器端)修改。

就你而言,你可以把這個方法放在/ collections或/ model文件夾中,不用擔心這個。你也可以把它放在/服務器上,事情也會起作用。

相關問題