node.js - 什么是 nodejs express 管理多个域?

标签 node.js express

我有 2 个域名。例如,example0.org 和 example1.org。 如何使用 express 设置 nodejs 来管理它们? 例如,我想分享

/publicexample0

根文件夹

example0.org

/publicexample1

文件夹

example1.org

作为根

这只适用于一个域:

var app = express.createServer();
app.get('/', function(req, res) {
    res.send('Hello World');
});
app.listen(3000);

最佳答案

我猜你能做的就是利用 HTTP host header .它包含:

The domain name of the server (for virtual hosting), and the TCP port number on which the server is listening. The port number may be omitted if the port is the standard port for the service requested. Mandatory since HTTP/1.1.

您可以在 RFC 2616 - HTTP v1.1 中查看其规范

显然,您可以从 Express 中的请求中读取 header ,并根据其值做出决定。

router.get('/hello', function(req, res){
   var host = req.get('host');
   if(host === 'example0.org'){
    res.send(200,'Welcome from example0.org');
   } else if(host === 'example1.org'){
    res.send(200,'Welcome from example1.org');
   } else {
    res.send(200,'Welcome stranger');
   }
});

关于node.js - 什么是 nodejs express 管理多个域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24015582/

相关文章:

node.js - 错误: Could not find module `eventsource` imported from a route

javascript - 在 Node.js 中,为什么使用 require 函数在模块之间变量相等?

node.js - 如何使用Node+Express渲染多个 View

node.js - Express - 多个角度应用程序

reactjs - node express Server 不提供带有 brotli 和 gzip 压缩的压缩静态文件

php - Node.js在没有任何操作的情况下被mysql自动崩溃

javascript - 如何在 Next.js 中获取仅限 http 的 cookie 值?

javascript - 从变量(从导入的 JS)中获取字符串作为对象中的键

node.js - React 中删除请求

javascript - 为什么我不断收到 TypeError : User is not a constructor?