Skip to content

ryanburnette/url-path-to-local-path

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

repo npm

Safely convert req.path into a local path.

Goals

  • 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 is index.md
  • Option to test for a series of local extensions ['.md', '.html']
  • Protect from path traversal

Usage

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();
    });
};

Options

  • 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

About

Safely convert req.path into a local path.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published