Express 从子目录提供动态子域内容
我的服务器上有一个目录结构,如:Express 从子目录提供动态子域内容,express,Express,我的服务器上有一个目录结构,如: site-a/app/index.html site-a/app/main.js site-a/app/vendor.js site-b/app/index.html site-b/app/main.js site-b/app/vendor.js 等等 我希望能够创建一个能够响应以下内容的服务器: curlhttp://site-a/app/ curlhttp://site-a/app/vendor.js 对于index.html文件,我有类似的东西,它可以工
site-a/app/index.html
site-a/app/main.js
site-a/app/vendor.js
site-b/app/index.html
site-b/app/main.js
site-b/app/vendor.js
等等
我希望能够创建一个能够响应以下内容的服务器:
curlhttp://site-a/app/
curlhttp://site-a/app/vendor.js
对于index.html
文件,我有类似的东西,它可以工作:
this.app.use((req, res) => {
res.sendFile(path.join(process.env['SOURCE'], req.subdomains[req.subdomains.length - 1], '/app/index.html'));
});
但是我一辈子都不知道如何让express.static在这样的情况下工作
奖励积分:任何失败的请求都应加载/app/index.html
。这是一个角度的应用
有人有什么建议吗?也许这就是你要找的
app.use('/app',函数(req,res,next){
让host=req.get('host');//由于某种原因,子域不能在localhost上工作
let site=host.includes('site-b')?'site-b':'site-a';//条件逻辑,因为^^^
让files=path.join(process.env['SOURCE'],site'app');//该站点/app文件夹的文件路径
express.static(文件)(req、res、function(){
res.sendFile(path.join(文件'index.html'))//回退到index.html
})
});
express.static
需要指定目录,而不是index.html
。它将自动使用index.html
作为根目录
我将下一个函数指定给express.static
,如果它找不到文件,就发送index.html
如果你有任何问题,请告诉我
/app/site-a
=http://site-a.localhost:port/app
/app/site-b
=http://site-b.localhost:port/app
也许这就是你要找的
app.use('/app',函数(req,res,next){
让host=req.get('host');//由于某种原因,子域不能在localhost上工作
let site=host.includes('site-b')?'site-b':'site-a';//条件逻辑,因为^^^
让files=path.join(process.env['SOURCE'],site'app');//该站点/app文件夹的文件路径
express.static(文件)(req、res、function(){
res.sendFile(path.join(文件'index.html'))//回退到index.html
})
});
express.static
需要指定目录,而不是index.html
。它将自动使用index.html
作为根目录
我将下一个函数指定给express.static
,如果它找不到文件,就发送index.html
如果你有任何问题,请告诉我
/app/site-a
=http://site-a.localhost:port/app
/app/site-b
=http://site-b.localhost:port/app