[英]ExpressJS - Serving Multiple Web Apps with Different Base Directories on the Same Express Server
I am trying to serve a website with different static directories for different routes. 我正在尝试为具有不同静态目录的网站提供不同的路由。
If a get request is made to the /tools*
route, I would like to use the /dist/toolsApp/
directory as the base directory for my frontend code. 如果对
/tools*
路由提出了获取请求,我想将/dist/toolsApp/
目录用作前端代码的基本目录。
If a get request is made to the /net*
route, I would like to use the /dist/netIdApp/
directory as the base directory for my frontend code. 如果对
/net*
路由提出了获取请求,我想将/dist/netIdApp/
目录用作前端代码的基本目录。
If a get request is made to the * route, I would like to use the /dist/homeApp/
directory as the base directory for my frontend code. 如果对*路由进行了获取请求,我想将
/dist/homeApp/
目录用作前端代码的基本目录。
I'm having difficulty determining where to insert the app.use(express.static(__dirname + DIRECTORY));
我很难确定在哪里插入
app.use(express.static(__dirname + DIRECTORY));
lines of code. 代码行。 I initially was trying the below code, but quickly realized that this code wasn't right because I do not want to merge the directories (they have conflicting file names).
我最初尝试下面的代码,但很快意识到该代码不合适,因为我不想合并目录(它们具有冲突的文件名)。
// Tools Routes
app.use(express.static(__dirname + '/dist/toolsApp/'));
app.get('/tools*', function(req, res) {
res.sendFile(path.join(__dirname + '/dist/toolsApp/index.html'));
});
app.use(express.static(__dirname + '/dist/netIdApp/'));
// Net-Id Authenticated Routes
app.get('/net*', function(req, res) {
res.sendFile(path.join(__dirname + '/dist/netIdApp/index.html'));
});
app.use(express.static(__dirname + '/dist/homeApp'));
// Default Route
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname + '/dist/homeApp/index.html'));
});
I then tried specify a route for each of the app.uses, as shown below. 然后,我尝试为每个app.uses指定一条路由,如下所示。 And that only seemed to ever send the last route (I also tried changing the last app.use from '*' to '/' and that didn't change anything).
而且这似乎只发送了最后一条路由(我还尝试将最后一个app.use从'*'更改为'/',并且没有任何改变)。 The result of this is that the frontend get requests end up having a '/' appended after the file name, making it so that the Express server interprets the request to be for a directory, not a regular file.
这样的结果是,前端获取请求最终在文件名后附加了一个“ /”,从而使Express服务器将请求解释为针对目录而不是常规文件。 I'm really unsure as to why this is happening.
我真的不确定为什么会这样。
// Tools Routes
app.use('/tools*', express.static(__dirname + '/dist/toolsApp/'));
app.get('/tools*', function(req, res) {
res.sendFile(path.join(__dirname + '/dist/toolsApp/index.html'));
});
app.use('/net*', express.static(__dirname + '/dist/netIdApp/'));
// Net-Id Authenticated Routes
app.get('/net*', function(req, res) {
res.sendFile(path.join(__dirname + '/dist/netIdApp/index.html'));
});
app.use('*', express.static(__dirname + '/dist/homeApp'));
// Default Route
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname + '/dist/homeApp/index.html'));
});
Lastly, I've tried putting the line within the respective app.get, but that wasn't working correctly (express doesn't send the frontend files). 最后,我尝试将代码行放入相应的app.get中,但这不能正常工作(express不会发送前端文件)。
// Tools Routes
app.get('/tools*', function(req, res) {
app.use(express.static(__dirname + '/dist/toolsApp/'));
res.sendFile(path.join(__dirname + '/dist/toolsApp/index.html'));
});
// Net-Id Authenticated Routes
app.get('/net*', function(req, res) {
app.use(express.static(__dirname + '/dist/netIdApp/'));
res.sendFile(path.join(__dirname + '/dist/netIdApp/index.html'));
});
// Default Route
app.get('*', function(req, res) {
app.use(express.static(__dirname + '/dist/homeApp'));
res.sendFile(path.join(__dirname + '/dist/homeApp/index.html'));
});
Could someone help me determine what the best way to do this is? 有人可以帮助我确定最好的方法是什么吗? Node: v8.11.2 Express:
节点:v8.11.2 Express:
Was able to resolve the issue, the following code did work, it just puts the files in the address.com/tools/, address.com/net/, and address.com/, respectively. 能够解决此问题,以下代码可以正常工作,只是将文件分别放在address.com/tools/、address.com/net/和address.com/中。
// Tools Routes
app.use('/tools*', express.static(__dirname + '/dist/toolsApp/'));
app.get('/tools*', function(req, res) {
res.sendFile(path.join(__dirname + '/dist/toolsApp/index.html'));
});
app.use('/net*', express.static(__dirname + '/dist/netIdApp/'));
// Net-Id Authenticated Routes
app.get('/net*', function(req, res) {
res.sendFile(path.join(__dirname + '/dist/netIdApp/index.html'));
});
app.use('*', express.static(__dirname + '/dist/homeApp'));
// Default Route
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname + '/dist/homeApp/index.html'));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.