UNPKG

five-server

Version:

Development Server with Live Reload Capability. (Maintained Fork of Live Server)

34 lines 1.57 kB
"use strict"; /** * @author Yannick Deubel (https://github.com/yandeu) * @copyright Copyright (c) 2021 Yannick Deubel * @license {@link https://github.com/yandeu/five-server/blob/main/LICENSE LICENSE} */ Object.defineProperty(exports, "__esModule", { value: true }); exports.notFound = void 0; const path_1 = require("path"); const public_1 = require("../public"); const misc_1 = require("../misc"); const explorer_1 = require("./explorer"); const notFound = (root, baseURL) => { return async (req, res, next) => { // join / normalize from root dir const path = (0, path_1.normalize)((0, path_1.join)(root, req.url)); const file = req.url.replace(/^\//gm, ''); // could be c:/Users/USERNAME/Desktop/website/ for example if (await (0, misc_1.fileDoesExist)(file)) { const html = public_1.STATUS_CODE.replace('{linked-path}', (0, explorer_1.htmlPath)(decodeURI(req.url), baseURL)) .replace('{status}', '403') .replace('{message}', `Can't access files outside of root.`); return res.status(403).send(html); } if (!(await (0, misc_1.fileDoesExist)(path))) { const html = public_1.STATUS_CODE.replace('{linked-path}', (0, explorer_1.htmlPath)(decodeURI(req.url), baseURL)) .replace('{status}', '404') .replace('{message}', 'This page could not be found.'); return res.status(404).send(html); } return next(); }; }; exports.notFound = notFound; //# sourceMappingURL=notFound.js.map