13

顧客がドメイン内の組織名を使用して SaaS にアクセスできるようにするにはどうすればよいですか?

たとえば、Web アプリ example.com には、OrgA と OrbB の 2 つの顧客がいるとします。

ログインすると、各顧客は自分のサイト orga.example.com / orgb.example.com にリダイレクトされます。

サブドメインを含むリクエストがノード サーバーに到達したら、単一の「/」ルートでリクエストを処理したいと考えています。ルート ハンドラー内では、ホスト ヘッダーを検査し、サブドメインを組織のパラメーターとして扱います。何かのようなもの:

app.get "/*", app.restricted, (req, res) ->
  console.log "/* hit with #{req.url} from #{req.headers.host}"
  domains = req.headers.host.split "."
  if domains
    org = domains[0]
    console.log org
    # TODO. do something with the org name (e.g. load specific org preferences)
  res.render "app/index", { layout: "app/app" }

注意。domain 配列の最初の項目は組織名です。ホスト ヘッダーにポートが表示されないことを前提としており、今のところ、非組織のサブ ドメイン名 (www、ブログなど) の処理方法を検討していません。

したがって、私が持っている質問は、さまざまなホスト ヘッダーを持つ要求を処理するように node/express を構成する方法についてです。これは通常、Apache ではワイルドカード エイリアスを使用するか、IIS ではホスト ヘッダーを使用して解決されます。

Apache/Rails の例は @ http://37signals.com/svn/posts/1512-how-to-do-basecamp-style-subdomains-in-railsです。

ノードで同じことをどのように達成できますか?

4

3 に答える 3

7

Express.vhost を使用したくない場合は、http-proxy を使用して、より組織化されたルーティング/ポート システムを実現できます。

var express = require('express')
var app = express()
var fs = require('fs')

/*
Because of the way nodejitsu deals with ports, you have to put 
your proxy first, otherwise the first created webserver with an 
accessible port will be picked as the default.

As long as the port numbers here correspond with the listening 
servers below, you should be good to go. 
*/

var proxyopts = {
  router: {
    // dev
    'one.localhost': '127.0.0.1:3000',
    'two.localhost': '127.0.0.1:5000',
    // production
    'one.domain.in': '127.0.0.1:3000',
    'two.domain.in': '127.0.0.1:4000',

  }
}

var proxy = require('http-proxy')
  .createServer(proxyopts) // out port
  // port 4000 becomes the only 'entry point' for the other apps on different ports 
  .listen(4000); // in port


var one = express()
  .get('/', function(req, res) {
   var hostport = req.headers.host
   res.end(hostport)
 })
 .listen(3000)


var two = express()
  .get('/', function(req, res) {
    res.end('I AM APP FIVE THOUSAND')
  })
  .listen(5000)
于 2012-12-15T23:28:09.870 に答える
2

ノードサーバーのIPアドレスとポートに到着するすべてのリクエストは、ノードサーバーによって処理される必要があると思います。そのため、apacheでvhostを作成して、たとえばサブドメインによってapacheが受信するリクエストを区別します。

サブドメインの処理方法を確認したい場合は、 express-subdomainsのソースコードを参照してください(ソースはわずか41行です)。

于 2012-01-16T17:10:13.083 に答える