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

remount-router

v0.2.1

Published

A simple file-based router for Express.js with automatic routes remounting on file changes without the need to restart the server.

Downloads

15

Readme

remount-router

A simple file-based router for Express.js with automatic routes remounting on file changes without the need to restart the server.

Quick Demo

Installation

npm install remount-router --save

or

yarn add remount-router

Initialization

The remount-router comes as middleware and takes a couple of options. Take this example server for instance:

const path = require('path')
const express = require('express')
const remountRouter = require('remount-router')

const app = express()

app.use(remountRouter({
  expressAppInstance: app,
  apiEndpoint: '/api',
  controllersPath: path.join(__dirname, 'controllers')
}))

app.listen(17777)

This will parse all *.js files in the ./controllers directory and mounts it to the /api endpoint.

Controllers

The name of a controller file will be used as the parent route. Controllers must export an Object, the keys define the HTTP verb and the route. Each verb/route can have one Function or an Array of Functions.

Let's take the controller file ./controllers/users.js for example:

module.exports = {
  'GET /' (req, res) {
    res.json({ action: 'find all users' })
  },

  'POST /admin/:id': [
    (req, res, next) => {
      // middleware to be executed before the route function
      next()
    },

    (req, res) => {
      res.json({ action: 'save a new user' })
    }
  ]
}

This will result in two routes: GET /api/users and POST /api/users/admin/:id. Note that you can use middleware if you pass an Array to the verb/route key.

The verb (GET, POST, PUT, etc.) can be any routing method offered by Express.js (app.get(), app.post(), app.put(), etc.).

Automatic Route Remounting

If the option isDev is true, all matching controller files will be watched for changes (adding/removing files, editing files). If a change is detected, a new router is generated and remounted to the apiEndpoint.

Options

Options are passed to remount-router as an Object (as seen in the initialization example).

const defaults = {
  expressAppInstance: null,
  controllersPath: path.join(process.cwd(), 'controllers'),
  controllersGlob: '*.js',
  apiEndpoint: '/api',
  isDev: !(process.env.NODE_ENV === 'production')
}

expressAppInstance (required)

The instance of your main app created with express(). This is used to initially mount the router. But more importantly, it is used to remount the router (there is no other way than passing it as an option).

controllersPath

Where to find controller files to parse. Note that the default is pointing to the controllers directory in the current working directory. This is convenient to quickly get started, but the process.cwd() might change when you deploy to another system. Therefor it is advised to overwrite this option with an absolute path (as seen in the initialization example).

controllersGlob

This defaults to *.js and is consumed by the Glob module. Set this option if you do not have controllers only in the controllers directory. You might have *.test.js files along the controllers, then you would exclude them by overwriting this option with *!(.test).js.

apiEndpoint

This defaults to /api and is the endpoint where the router is mounted and all sub-routes parsed from the controllers will be mounted on. Remember, a controller named ./controllers/posts.js with a route key GET /all will result in the GET route /api/posts/all.

isDev

This defaults to true if the NODE_ENV environment variable is not production, otherwise it is false. File watching and route remounting will only happen if this option is true.

Using it with Nuxt.js

Implementing remount-router in Nuxt.js is super simple. After you've installed the module (yarn add remount-router), add it to your nuxt.config.js as a module like so:

module.exports = {
  modules: [
    'remount-router/nuxt'
  ]
}

Notice that we don't pass any options: the expressAppInstance is created for you, and the controllersPath is set to /controllers inside Nuxt's srcDir.

However, you can pass custom options like so:

module.exports = {
  modules: [
    ['remount-router/nuxt', {
      controllersGlob: '*!(.test).js',
      apiEndpoint: '/api/v1'
    }]
  ]
}

Development

yarn
yarn test

Changelog

  • 2017-08-29: Add Nuxt.js module wrapper
  • 2017-08-25: Initial release

License

Copyright 2017 Sebastian Senf (enkrateia.me)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.