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

fast-custom-router

v1.4.2

Published

Fast custom express router.

Downloads

61

Readme

release version downloads Jest - Unit tests Node.js Package

import { Parser } from 'fast-custom-router'

const parser = new Parser(app, config)
parser.parseFromFile('config.yml')
parser.load()

fast-custom-router

This is a powerful custom enrober for express router.

Installation

This is a Node.js module available through the npm registry.

Before installing, download and install Node.js v14.X.X or higher.

Installation is done using the npm install command:

npm install fast-custom-router

Features

  • strict configuration parser
  • dynamic module imports (middlewares and controllers)
  • type checking (uri params and body params)
  • modular configuration files (with imports)

Quick start

First of all, you will need to use a router such as Express.

Then create one configuration file, for example:

# config/config.yaml

api:
  root: /api
  pre_middlewares:
    - authenticate
  post_middlewares:
    - errorHandler
  routes:
    user:
      path: /user/:id
      params:
        id: number
      methods:
        GET:
          controller: user/user:getUser
        DELETE:
          controller: user/deleteUser
          pre_middlewares:
            - checkUserPermissions
          post_middlewares:
            - logRequests

Then your main script launcher:

// main.js

import { dirname, join } from 'path'
import { fileURLToPath } from 'url'

import http from 'http'
import Express from 'express'

import { Parser as APIParser } from 'fast-custom-router'

/** Configuration */
const __dirname = dirname(fileURLToPath(import.meta.url))
const config = {
  config_dir: join(__dirname, 'config'),
  controller_dir: join(__dirname, 'controller'),
  middleware_dir: join(__dirname, 'middleware'),
}

/** Define servers */
const app = Express()
const server = http.createServer(app)
const parser = new APIParser(app, config)

parser.parseFromFile('config.yaml')
parser.load()

server.listen(8000, () => {
  console.log('[SERVER][INFO] Server started on port 8000')
})

And finnaly create middlewares and controllers

// middleware/authenticate.js
export default function authenticate(req, res, next) {
  // Doing stuff here
  next()
}

// [...]

// controller/user.js
export function getUser({params}) {
  return {
    message: `User #${params.id} has been requested !`
  }
}

// controller/deleteUser.js
export default function deleteUser({params}) {
  return {
    message: `User #${params.id} has been deleted !`
  }
}

Documentation

Philosophy

The package philosophy is to provide small a smart configuration parser that load an application into a router. The package must be robust, fast (in execution time way), and fast (in writting code way).

The key words are :

  • robust
  • modularity
  • easy-to-use
  • light

It does not depends on any specific router, but we recommand to use with Express.

Tests

The library provide tests, and every commit is checked by using Github Actions : Jest - Unit tests.

If you want to run the tests by yourself, please follow this commands :

git clone [email protected]:vigilock/fast-custom-router.git
cd fast-custom-router
npm install
npm test

License

At Consignity we chosen the MIT License as open-sourced license.