Add rewrite url for articles relative resources
This commit is contained in:
+8
-1
@@ -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
|
// catch all article urls and render them
|
||||||
app.get('*', (req, res, next) => {
|
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 articlePath = req.path.substr(1, 10);
|
||||||
const article = articles[articlePath];
|
const article = articles[articlePath];
|
||||||
if (!article)
|
if (!article)
|
||||||
|
|||||||
+10
-1
@@ -286,13 +286,22 @@ describe('Test static files', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
test('200 valid file', (done) => {
|
test('200 valid file', (done) => {
|
||||||
fs.writeFileSync(`${dataDir}/somefile.txt`, 'filecontent');
|
fs.writeFileSync(path.join(dataDir, 'somefile.txt'), 'filecontent');
|
||||||
request(app).get('/somefile.txt').then((response) => {
|
request(app).get('/somefile.txt').then((response) => {
|
||||||
expect(response.statusCode).toBe(200);
|
expect(response.statusCode).toBe(200);
|
||||||
expect(response.text).toBe('filecontent');
|
expect(response.text).toBe('filecontent');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
test('200 valid resource of article', (done) => {
|
||||||
|
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
|
||||||
|
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'somefile.txt'), 'filecontent');
|
||||||
|
request(app).get('/2019/05/05/title/somefile.txt').then((response) => {
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
expect(response.text).toBe('filecontent');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Test other requests', () => {
|
describe('Test other requests', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user