1
0
Fork 0
forked from svrjs/svrjs
This repository has been archived on 2024-11-10. You can view files and clone it, but cannot push or open issues or pull requests.
svrjs/src/middleware/redirectTrailingSlashes.js

36 lines
890 B
JavaScript
Raw Normal View History

const fs = require("fs");
module.exports = (req, res, logFacilities, config, next) => {
// Trailing slash redirection
2024-08-24 20:45:17 +02:00
if (
!req.isProxy &&
!config.disableTrailingSlashRedirects &&
req.parsedURL.pathname[req.parsedURL.pathname.length - 1] != "/" &&
req.originalParsedURL.pathname[req.originalParsedURL.pathname.length - 1] !=
"/"
) {
fs.stat(
"." + decodeURIComponent(req.parsedURL.pathname),
function (err, stats) {
if (err || !stats.isDirectory()) {
try {
next();
} catch (err) {
res.error(500, err);
}
2024-08-24 20:45:17 +02:00
} else {
res.redirect(
req.originalParsedURL.pathname +
"/" +
req.parsedURL.search +
req.parsedURL.hash,
);
}
},
);
} else {
next();
}
};
2024-08-25 12:37:14 +02:00
module.exports.proxySafe = true;