Safely convert req.path
into a local path.
- Consistent behavior with
/foo
and/foo/
- Consistent behavior with
/foo
finding/foo.html
- Check for existence of the file
- Set a different extension for local file, req.path might be
index.html
while local file isindex.md
- Option to test for a series of local extensions
['.md', '.html']
- Protect from path traversal
var UrlPathToLocalPath = require('@ryanburnette/url-path-to-local-path');
var getLocalPath = UrlPathToLocalPath({
context: './content/', // where is the content?
localExtension: '.ejs' // or use localExtensions with an Array to try multiple extensions
});
// Express middleware example
module.exports = function (req, res, next) {
getLocalPath(req.path)
.then(function (localPath) {
return fs.promises.readFile(localPath, 'utf8');
})
.then(function (html) {
res.send(html);
})
// throws an error if no local path found, we'll catch that
.catch(function (error) {
next();
});
};
context
Working directory for content.localExtension
If the local files have a different extension such as.md
or.ejs
localExtensions
Use this with an array if multiple extensions should be tested