feat/docs: added custom 404 pages (closes #174)

pull/190/head
tycrek 2 years ago
parent 6e74f2ad45
commit d44f26091d
No known key found for this signature in database
GPG Key ID: FF8A54DCE404885A

6
.github/README.md vendored

@ -326,6 +326,12 @@ By default, ass directs the index route `/` to this README. Follow these steps t
module.exports = (req, res, next) => res.redirect('/register'); module.exports = (req, res, next) => res.redirect('/register');
``` ```
## Custom 404 page
To use a custom 404 page, create a file in the `share/` directory called `404.html`. Restart ass, and any requests to missing resources will return HTTP 404 with the contents of this file.
If there's interest, I may allow making this a function, similar to the custom index.
## File storage ## File storage
ass supports three methods of file storage: local, S3, or [Skynet]. ass supports three methods of file storage: local, S3, or [Skynet].

@ -22,14 +22,30 @@ if (fs.existsSync(path('share/', 'theme.json')))
theme = fs.readJsonSync(path('share/', 'theme.json')); theme = fs.readJsonSync(path('share/', 'theme.json'));
// Middleware for parsing the resource ID and handling 404 // Middleware for parsing the resource ID and handling 404
let custom404 = {
html: '',
checked: false,
error: null,
path: path('share/', '404.html')
};
router.use((req: Request, res: Response, next) => { router.use((req: Request, res: Response, next) => {
// Parse the resource ID // Parse the resource ID
req.ass = { resourceId: escape(req.resourceId || '').split('.')[0] }; req.ass = { resourceId: escape(req.resourceId || '').split('.')[0] };
// If the ID is invalid, return 404. Otherwise, continue normally // If the ID is invalid, return 404. Otherwise, continue normally
data().has(req.ass.resourceId) const processRequest = () => data().has(req.ass.resourceId)
.then((has: boolean) => has ? next() : res.sendStatus(CODE_NOT_FOUND)) // skipcq: JS-0229 .then((has: boolean) => has ? next() : custom404.html.length !== 0 ? res.status(CODE_NOT_FOUND).sendFile(custom404.path) : res.sendStatus(CODE_NOT_FOUND)) // skipcq: JS-0229
.catch(next); .catch(next);
// check if share/404.html exists
if (!custom404.checked)
fs.access(custom404.path, fs.constants.F_OK)
.then(() => fs.readFile(custom404.path, 'utf8'))
.then((data: string) => custom404.html = data)
.catch((err) => custom404.error = err)
.finally(() => (custom404.checked = true, log.debug('Custom 404', custom404.html.length !== 0 ? 'found' : 'not found', custom404.error ? `${custom404.error}` : 'no errors')))
.then(() => processRequest());
else processRequest();
}); });
// View file // View file

Loading…
Cancel
Save