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

express-path-route

v1.1.3

Published

(from: express-load-routes)Loads express.js routers from a given folder recursively and mounts them

Downloads

7

Readme

express-path-route

Notice:

This project is taken over from Gabriel Vaquer 's NPM package express-load-routes.

Just modified some code to conform with my usage.

Which is:

myapp
  |-> bin
  |-> public
  |-> routes
    |-> index.js
    |-> admin.js
    |-> admin
      |-> index.js
      |-> dashboard.js

I will load the routers in this order:

index.js -> admin.js -> admin/index.js -> admin/dashboard.js

In this way, I can put some path base middlewares to a assign path, such as in admin.js :


var auth = require('middlewares/auth');

router.use(auth);

When you use the express-generator package to create a new express.js application, you get a routes folder for free and some boilerplate code. Usually, you have to require all your routes and mount them. That's fine for small apps that only use a few routes, but for large applications this can be hell.

This module is supposed to make this task a little bit easyer by loading all files within a given folder (defaults to ./routes) and mounting them as routes -> router relatively to the ./routes folder.

For example:

myapp
  |-> bin
  |-> public
  |-> routes
    |-> index.js
    |-> users.js
    |-> admin
      |-> index.js
      |-> dashboard.js
      |-> customers.js

This folder structure would generate the following routes:

/
/users
/admin
/admin/dashboard
/admin/customers

Notice that index.js files are translated to root slashes /.

So, the only thing that this module is expecting is a Router (express.Router()) instance to work.

// routes/users.js
var express = require('express')
var router = express.Router();

// your routes goes here

module.exports = router;

This piece of code wraps the whole thing toggether. The following example is a common "resourcefull" route declaration.

// routes/users.js
var express = require('express')
var router = express.Router();

router
  
  .get('/', function(req, res) {
    User.find({}, function(err, users) {
      res.status(200).json(users);
    });
  })
  
  .get('/:id', function(req, res) {
    var id = req.params.id;
    User.findById(id, function(err, user) {
      if (err) throw err;
      if (!user) res.sendStatus(404);
      res.status(200).json(user);
    });
  })
  
  .post('/', function(req, res) {
    var body = req.body;
    var user = new User(body);
    user.save(function(err) {
      if (err) throw err;
      res.status(201).json(user);
    });
  })
  
  .patch('/:id', function(req, res) {
    var id = req.params.id;
    var body = req.body;
    User.findByIdAndUpdate(id, body, function(err, user) {
      if (err) throw err;
      res.sendStatus(200);
    });
  })
  
  .delete('/:id', function(req, res) {
    var id = req.params.id;
    User.findOneAndRemove(id, function(err) {
      if (err) throw err;
      res.sendStatus(200);
    });
  });

module.exports = router;

The above example would generate something like this:

GET    /users
GET    /users/:id
POST   /users
PATCH  /users/:id
DELETE /users/:id

Installation & Usage

npm install express-path-route --save
// app.js
var express = require('express');
var app = express();

// middlewares goes here
app.use(express.static(__dirname, '/public'));
// ...

// require the module and pass the 
// express instance
require('express-path-route')(app);

// if you don't use the default routes folder
// you can specify the path to your own
// as the second argument
require('express-path-route')(app, './path/to/routes');

module.exports = app;

Hope you like it. Cheers.