Add rewrite url for articles relative resources

This commit is contained in:
Clément GOUIN
2019-06-21 09:09:08 +02:00
parent 00d1d6c4e1
commit 5fb8e0fa45
2 changed files with 18 additions and 2 deletions
+8 -1
View File
@@ -161,9 +161,16 @@ module.exports = (config) => {
}
});
//rewrite urls to hide articles titles : /2019/05/05/sometitle/img.png => /2019/05/05/img.png
app.use((req, res, next) => {
if (/^\/\d{4}\/\d{2}\/\d{2}\//.test(req.url))
req.url = req.url.slice(0, 11) + req.url.slice(req.url.lastIndexOf('/'));
next();
});
// catch all article urls and render them
app.get('*', (req, res, next) => {
if (/^\/\d{4}\/\d{2}\/\d{2}\/(\w*\/)?$/.test(req.path)) {
if (/^\/\d{4}\/\d{2}\/\d{2}\/$/.test(req.path)) {
const articlePath = req.path.substr(1, 10);
const article = articles[articlePath];
if (!article)