我有 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/