Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何使用Node和Express创建客户/客户机样式的子域_Node.js_Express_Connect_Hostheaders - Fatal编程技术网

Node.js 如何使用Node和Express创建客户/客户机样式的子域

Node.js 如何使用Node和Express创建客户/客户机样式的子域,node.js,express,connect,hostheaders,Node.js,Express,Connect,Hostheaders,如何允许客户在域中使用其组织名称访问SaaS 例如,一个web应用程序example.com可能有两个客户,OrgA和OrbB 登录后,每个客户都会被重定向到其网站orga.example.com/orgb.example.com 一旦包含子域的请求到达节点服务器,我希望通过单个“/”路由处理该请求。在路由处理程序中,它只检查主机头并将子域视为组织的参数。 比如: app.get "/*", app.restricted, (req, res) -> console.log "/* h

如何允许客户在域中使用其组织名称访问SaaS

例如,一个web应用程序example.com可能有两个客户,OrgA和OrbB

登录后,每个客户都会被重定向到其网站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" }
注意。域数组中的第一项是组织名称。我假设主机头中没有出现端口,目前我没有考虑如何处理非组织子域名(如www、blog等)

因此,我的问题更多地是关于如何配置node/express来处理具有不同主机头的请求。这通常在Apache中使用通配符别名或在IIS中使用主机头来解决

Apache/Rails的一个示例是@


如何在node中实现同样的功能?

我认为任何到达节点服务器的IP地址和端口的请求都应该由节点服务器处理。这就是为什么要在apache中创建vhost,以区分apache接收的请求(例如子域)


如果您想了解它如何处理子域(仅41行),请查看源代码。

我也有类似的情况,尽管我有一个主站点example.com,用户可以在其中登录和管理他们的站点,然后OrgA.example.com是一个面向公众的网站,将根据登录用户在example.com上的操作进行更新


我最终创建了两个独立的应用程序,并在connect中设置虚拟主机,将“example.com”指向一个应用程序,将“*”指向另一个应用程序。如果您不想使用express.vhost,可以使用http代理来实现更有序的路由/端口系统

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)

您可以编写一个中间件,查看主机头,获取该特定主机所需的任何数据,并将其添加到req对象中,以便在路由/中间件中使用。我不清楚如何设置服务器以捕获*.example.com。我认为可能需要一个代理(),但在我试图理解它之前,我需要了解更多。我还需要解决方案,让我在本地开发(在OSX上)。