2015-09-16 56 views
0

我想創建一個API,它會發出很多HTTP請求並處理這些請求的數據。我想用Meteor應用程序來呈現結果,但Node.js似乎更適合前者。讓一個Node.js應用程序和一個Meteor應用程序同時運行是明智的做法,還是會有不可避免的性能損失?如果確定,連接它們的最佳方式是什麼?在同一臺服務器上連接Node.js和Meteor

  • 向本地Node.js服務器發出HTTP請求。
  • Have Meteor在MongoDB數據庫中寫入一個條目,Node.js應用程序觀察這些條目以進行更改。

哪一個更可取?如果是這樣的話,還是還有其他選擇?

+1

流星可與節點非常偉大的JS,你可以創建流星包,並使用相同的文件中的所有節點JS美容:)如果你需要的例子,我告訴你的東西回答 –

+0

是的,請。那太好了!我希望你的解決方案允許通過npm添加Node.js pacakges(對於Node.js pacakage)。另外,我在網上找到的關於添加節點包的所有內容都需要將它們上傳到npm,我希望有一種方法可以繞過這一點。 – Alex

+0

發現這個http://stackoverflow.com/questions/27308965/is-it-possible-to-use-local-unpublished-node-modules-in-meteor-apps。雖然我沒有定義'模塊',所以會有更詳細的指導。 – Alex

回答

1

有一個npm package to talk over DDP via a node application

但是,如果你想處理HTTP請求,你可以簡單地使用流星的WebApp包來處理HTTP請求,並且會以傳統的「類節點」方式作出反應。 req是一個Node請求對象,而res是一個Node響應對象。嘗試是這樣的:

WebApp.connectHandlers.use('/api/v1/things', function (req, res, next) { 
    if (req.method === 'GET') { 
    res.writeHead(200, { 'Content-Type': 'application/json' }); 
    res.write(JSON.stringify(Things.find().fetch())); 
    res.end() 
    } 
    next() 
}); 
0

下面是關於如何使用Node.js和受流星爲例一起希望這是有幫助的

您應該創建一個流星包它可以讓你require NPM模塊,用Npm.require('name'),它是流星的方式來管理類似於npm包的代碼。 http://docs.meteor.com/#/full/packagejs

這裏是一個簡單的包:

/package_demo 
/package_demo/package.js 
/package_demo/server.js 

package.js

// Standart package.js file with some of the options 
Package.describe({ 
    name: 'username:packagename', 
    summary: 'what this does', 
    version: '0.0.1' 
}); 

// if you need any global/core or anything from npmjs.com 
// it will make it available for us to use like Npm.require 
// in the package files 
Npm.depends({ 
    'request': '2.62.0' 
}) 

Package.onUse(function (api) { 
    // we are going to use mongo so we need to specify it 
    api.use('mongo', 'server'); 

    // adding package files 
    api.addFiles('server.js', 'server'); 

    // exporting the mongo collection to make it available to the meteor app 
    api.export('github', 'server'); 
}); 

server.js:

// we can require any npm modules we specify at package.js 
var request = Npm.require('request') 

// meteor code 
// new mongo collection not needed if you defined it elsewhere 
github = new Mongo.Collection('github'); 

// we wrap this with startup to run after the meteor server process is finished starting 
// this makes sure github collection exists, if you have external collection 
// that you connect with DDP or you know is available you don't have to use it. 
Meteor.startup(function() { 
    console.log('server started'); 

    // node js code - you can use the full power of node async 
    // query github api for "meteor" repo 
    request({ 
     url: 'https://api.github.com/repos/meteor/meteor', 
     headers: { 
      'User-Agent': 'request' 
     } 
    }, 

    // regular node js request callback 
    // but we made it compatible with meteor with Meteor.bindEnvironment(callback); 
    // it makes sure we have access inside the callback to github mongo collection 
    // always wrap callbacks to non-Meteor libraries with Meteor.bindEnvironment 
    // if you need access to meteor functions/objects etc... if we just wanted to 
    // console.log the information it can work without Meteor.bindEnvironment() 
    Meteor.bindEnvironment(function (error, response, body) { 
     if (!error && response.statusCode === 200 || response.statusCode === 304) { 
      var data = JSON.parse(body); 
      console.log(data.stargazers_count + ' Stars'); 

      // meteor code 
      // insert it to meteor collection 
      github.insert({ id: data.id, fullName: data.full_name, stars: data.stargazers_count }); 
     } 
    })); 
}); 

我看見你也想require本地NPM模塊,我想你可能會破解你的方式,並將其與一起加入10但我建議不要這樣做,因爲當你編譯你的項目並投入生產時,獲得路徑並不是可靠的途徑,並且可能會破壞。因此,您並不需要發佈npm模塊來需要它,您也可以將其全局安裝在機器上

在您完成npm init並創建了npm軟件包全局安裝後,您的軟件包根目錄中的npm軟件包npm install . -g然後可以驗證它是否全局存在npm ls -g之後,您可以將自己的模塊包含在與上面相同的流星包中。

此外,您可能根本不需要創建節點js包,也可以將更多文件添加到流星包api.addFiles(['server.js', 'server2.js'], 'server');,並且其中的代碼可以是節點js代碼(不支持nodejs導出),如果需要導出對象或什麼是全局可在您的流星應用程序,你可以使用api.export();

因爲共享代碼例如軟件包文件:

服務器2。JS

// code between the package files is shared 
githubAPI.query(); 

並加入這server.js

githubAPI = { 
    query: function() { 
     request({ 
      url: 'https://api.github.com/repos/meteor/meteor', 
      headers: { 
       'User-Agent': 'request' 
      } 
     }, Meteor.bindEnvironment(function (error, response, body) { 
      if (!error && response.statusCode === 200 || response.statusCode === 304) { 
       var data = JSON.parse(body); 
       console.log(data.stargazers_count + ' Stars'); 
       github.insert({ id: data.id, fullName: data.full_name, stars: data.stargazers_count }); 
      } 
     })); 
    } 
}; 

控制檯輸出:登錄+插入到數據庫 2倍。所以,這就像要求你剛纔添加的文件:)

28212 Stars 
28212 Stars 
相關問題