npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dword-design/express-mount-files

v0.0.4

Published

An express middleware for registering routes through your filesystem

Downloads

8

Readme

express-mount-files

Travis CI - Build Status Appveyor - Build status Coverage Status

An express middleware that lets you match your folder structure to your app's URL structure. Declare routes by creating files at the path you want them to handle requests for, helping identify which bit of the app is handling a request hitting a specific URL.

Getting started

Add the middleware to your express app, passing the folder that'll contain your routes.

const mw = require('express-mount-files');
// Use the routes folder as the root for all your routes
app.use(mw('routes'))

In that routes folder, create files ending with <http_method>.js and exporting the function that will handle the request. You can use any of the HTTP methods supported by Express. They need to be in lowercase in the file name, though.

For example, the following code in routes/hello/world.get.js will make GET requests to /hello/world respond with 'Hello world'.

module.exports = function(req,res) {
  res.send('Hello world')
}

The middleware also support files that don't have any part before their <http_method>.js extension. For example 'routes/hello/get.js' will respond to GET requests sent to /hello.

Mounting order

Deeper routes will be mounted first ensuring requests don't get swallowed by shallow routes being mounted ahead.

Template rendering

If a specific URL only needs to render a template, you can create files ending with <http_method>.<template_extension> to declare routes the same way as for JavaScript function.

For this to happen, you'll need to:

  • configure Express to render use the templating engine of your choice

  • let the middleware know which extension those templates are using, via its viewExtensions option. As Express allows multiple template engines, this option accepts an Array.

      // Provided `pug` has been registered as a view engine for Express
      const mw = require('express-mount-files');
      app.use(mw('routes', {viewExtensions: ['pug']}))

With that in place, the routes/time.get.pug template will render for requests hitting /time.

Mounting order

For a same path, JavaScript routes will always be mounted before template routes. This lets you take advantage of Express' next() function to:

  • prepare the data in a JavaScript route, say the greeting.get.js file

    module.exports = function(req,res,next) {
      res.locals.greeting = 'Howdy!';
    }
  • render the response in greeting.get.pug template

    h1 #{res.locals.greeting} world!

Route parameters

Express allows parts of routes starting in : to be intepreted as parameters for the requests, later available in the req.params object. This middleware provides the same feature, with one little tweak, it uses the $ character instead of : (: didn't feel that safe to put in a file/folder name).

This way, the file at routes/users/$userId/edit.get.js will get 12 for req.params.userId when responding to /users/12/edit.

Mounting order

For routes with the same depth (say /users/$userId/edit.get.js and /users/me/edit.get.js), the routes with the most variables will be mounted last, leaving the more specific routes to match first.

Mounting middlewares

express-mount-files also lets you set up middlewares of specific routes. It'll look for routes.js files exporting an Array of functions to be registered as middlewares.

The following routes/middlewares/routes.js will register two functions to be used as middlewares for requests hitting routes/middlewares.

module.exports = [function(req,res, next){
  next()
}, function(req,res,next) {
  next()
}];

Mounting order

The middlewares in those routes.js files are always mounted before the JavaScript and view routes. This means middlewares will run for all the routes within the folder. Contrary to the routes, though, middlewares will be mounted deepest last. This allows middlewares declared closest to the root to apply before the ones declared deeper.

Declaring middlewares and routes simmultaneously

The routes.js files can also export a hash, in which case they will be used to register:

  • middlewares declared in their use key
  • routes declared in any <http_method> key

For example, this routes.js file will mount:

  • 1 function as middleware,
  • two routes to handle the GET and POST requests hitting the path of the folder:
module.exports = {
  use: [function(req,res,next) {
    next()
  }],
  get(req,res) {
    res.send('GET')
  },
  post(req,res) {
    res.send('POST')
  }
}

Mounting order

The middlewares will be mounted in the same order as when exporting an Array. Routes will follow the same order as when using <http_method>.js files.

Folder separator

The middleware replaces any double underscore (__) as a directory separator. This should be used sparingly, but can help preventing to create nested folders for a single route file.