Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

express-custom-router

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-custom-router

Custom route matching for express.js

  • 1.0.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

express-custom-router

Custom routing rules for express.js.

Installation

npm install --save express-custom-router

Usage

express-custom-router differs from the standard Express router, in that accepts a match function in place of a route path. The mounted controller will only be called if the match function returns true.

This allows much more flexibility in route handling.

const express = require('express');
const CustomRouter = require('express-custom-router');

const app = express();
const router = CustomRouter();

// Define routing rules for a controller
router.get(
    // Matching function receives the request,
    // and returns true/false
	req => (
	  req.url.startsWith('/foo') &&
	  req.method === 'GET' &&
	  req.headers['x-my-request-header'] = 'quux'
	),
	// Controller
	(req, res, next) => res.send('foo')
);

// Mount the router
app.use(router);

Why?

I am working on an application with very complicated route, which can accept all sorts of different options, in all sorts of different formats. Relying on Express' routing syntax makes handling these route very difficult.

With express-custom-router, I can parse the url however I want to, and then route based on this parsed route.

For example:

const app = express();
const router = CustomRouter();

// Middleware for manually parsing
// request parameters
router.use((req, res, next) => {
  req.opts = manuallyParseMyUrl(req.originalUrl);
  next();
});

// Setup routes, based on my manually parsed params
router.get(
  req => req.opts.foo === 'bar',
  (req, res) => res.send('foo is bar')
);
router.get(
  req => req.opts.foo === 'quux',
  (req, res) => res.send('foo is quux')
);

Reference

CustomRouter#all(match, controller)

Mount a controller for any HTTP verb

CustomRouter#get(match, controller)

Mount a controller for GET requests

CustomRouter#post(match, controller)

Mount a controller for POST requests

CustomRouter#put(match, controller)

Mount a controller for PUT requests

CustomRouter#delete(match, controller)

Mount a controller for DELETE requests

CustomRouter#use(middleware)

Mount middleware for all requests. Middleware must use the format (req, res, next) => {}

Keywords

FAQs

Package last updated on 22 Jul 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc