Im using a set of static routes in Node Express and are experiencing a very strange phenomenon.
The routing is set up so that '/list/*'
and '/setup/*'
gets to different html-files where the directories are used kind of as storage id, for example updating info on the page url/setup/12345
would store info in 12345
.
To be able to load scripts and such there is also route with regex matching /assets/
to allow url/setup/assets/script.js
to be reached without routing to setup.html
with a new storage id.
My problem is that this works for url/setup/assets/script.js
but not for url/list/assets/script.js
even though they have identical routings.
Edit:
Navigating to url/list/assets/script.js
leads to list.html
(unwanted behaviour)
Navigating to url/setup/assets/script.js
leads to script.js
(wanted behaviour)
Any ideas on why '/list/*'` wont work?
Here are my static routes:
app.use(/assets/, express.static(wwwPath));
app.use('/list/*', function(req, res, next) {
res.sendFile('list.html', { root: wwwPath });
});
app.use('/setup/*', function(req, res, next) {
res.sendFile('setup.html', { root: wwwPath });
});
The solution was to use custom middleware. Here are the new routes:
var requestParser = function(req, res, next) {
if(req.originalUrl.indexOf('/assets/') >= 0) {
var assetPath = path.join(wwwPath, req.path.slice(req.url.indexOf('/assets/')));
fs.stat(assetPath, function(error, stat){
if(stat && stat.isFile()) {
res.sendFile(assetPath);
}
else{
res.status(404).send('<h1>404</h1>');
}
});
}
else {
next();
}
};
app.use(requestParser);
app.use('/list/*', function(req, res, next) {
res.sendFile('schema.html', { root: wwwPath });
});
app.use('/setup/*', function(req, res, next) {
res.sendFile('setup.html', { root: wwwPath });
});
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments