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

expressjs-router

v2.1.0

Published

Structured routes for ExpressJS

Downloads

39

Readme

Express.js Router Build Status

Structured routing for Express.JS

Example

Every route is implemented in a dedicated file

Example:

.
+-- routes
|   +-+ todos
|     +-- create.js
|     +-- update.js
|     +-- delete.js
+-- models
|   +-- todo.js
+-- server.js

Example route definition routes/todos/create.js

// File: ./routes/todos/create.js

'use strict';


const Todo = require("../../models/todo");



// The validate function is executed before the responder, allowing
// to easily check for the existence of parameters and their format.
// If some parameters are not valid, the function must throw an error
// 
// Note: Can be either synchronous or asynchronous.
function validate(req) {
  if (!req.body.text) {
    throw new Error('Missing parameter "text"')
  }
}


// The respond method is the actual implementation of our route
async function respond(req, res, next) {
  let { text } = req.body;
  let user = req.preloadedData.user;
  
  let todo = new Todo({
    text,
    user,
  });
  
  await todo.validate();
  await todo.save();
  
  res.status(201);
  res.json({ todo });
}



// The exports describe the route definition.
// .path, .method and .respond are required
// 
// Conditions are checked to define wether the route should be
// executed or not. You can define as many conditions as you want.
export default {
  path: '/todos',
  method: 'POST',

  validate
  respond,
  
  conditions : [ 'isLoggedIn' ],
  preload    : [ 'user' ],
};

Express app server.js

// File: ./app.js

'use strict';

import express from 'express'; 
import router from 'expressjs-router';

const app = express();


// By enabling debug mode, routes will be logged to stdout
router();

// Conditionals allow us to add handy condition checks all over our app
router.createConditional('isLoggedIn', async (req) => {
  return !!req.session.user;
});

// Preloaders allow us to easily preload resources before the requests are executed
router.createPreloader('user', async (req) => {
  return await User.findById(req.session.user._id);
});


// You can decide if either you want to preload resources sequentially or in parallel
//
// 0 -> parallel (default)
// 1 -> sequential
//
router.setPreloadingMode(1);


// Finally, create routes by padding the path where they're stored
router.create(app, "./path/to/routes");


app.listen(80);

Remarks:

  1. Routes prefixed by _ or named index.js won't be included
  2. You can enable debug mode by calling router.enableDebug();
  3. Route creation is recursive.

Changelog:

2.0.0

  • Removed express-validator
  • Made validate function support asynchronous execution (using Promises)
  • Added ALL to match any HTTP method
  • Allowed method to be an array of HTTP methods

License:

MIT