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-named-router

v0.0.2

Published

Named router for Express.js with Postman export

Downloads

2

Readme

expressjs-named-router

Named router for Express.js with support for Postman exports.

You can find a full example here: example

Installing

Using npm:

$ npm install expressjs-named-router

Usage

const express = require('express');
const app = express();
const server = require('http').createServer(app);

const host = '127.0.0.1';
const port = 3000;

var router = require('express-router');
app.use(router({
    baseUrl: "http://"+host+":"+port,
    directories: {
        routes: "app/routes",
        middlewares: "app/middleware"
    },
    postman: {
        appName: 'MyAppName',
        host: "{{myAppName.env.host}}",
        headers: {
            "Content-Type": "application/json",
            "User-Access-Token": "{{myAppName.user.token}}",
        },
        environment: {
            "myAppName.env.host": {
                "value": "http://"+host+":"+port,
                "description": "Host"
            },
            "myAppName.app.token": "",
            "myAppName.user.token": "",
        }
    }
},{/* options to pass in controller */}));

if(process.env === "DEV"){
    console.log(router.collections.routes());
    console.log(router.collections.middlewares());


    router.exportToPostman();
}

server.listen(port,host,function() {
    console.log('Server was started on http://%s:%s running on worker: %s', server.address().address, server.address().port , process.pid);
});

Creating a controller

module.exports = function(router){
    return {
        take: true,
        name: "app.dashboard",
        title: "Dashboard",
        description: "This is the application dashboard",
        route: "/",
        method: "GET",
        middleware: ["app.authorization"],
        fn: function(req, res){
            res.send("Hello World!");
        }
    };
};

| Parameter | Type | Description | | --- | --- | --- | | take | boolean | Registers the route or not (used in dev mode) | | name | string | The name of the route | | title | string | The title of the route (used in Postman) | | description | string | The description of the route (used in Postman) | | route | string | The endpoint | | method | string | The method of the route | | middleware | array | Array of middlewares | | fn | function | Callback executed when requesting the route |

Requesting the named route

Example:

router.urlFor(name,params);
module.exports = function(router){
    return {
        name: "user.account.view",
        title: "Account / View Account",
        route: "/account/:id",
        method: "GET",
        middleware: ["app.authorization","user.authorization"],
        fn: function(req, res){
            res.send("View account");
        }
    };
};
router.urlFor('user.account.view',{id: 1});
module.exports = function(router){
    return {
        name: "user.account.uploadImage",
        title: "Account / View Account",
        route: "/account/:id/upload-image",
        method: "POST",
        middleware: ["app.authorization","user.authorization"],
        fn: function(req, res){
            res.send("Image uploaded");
        }
    };
};
router.urlFor('user.account.uploadImage',{id: 1, action: "upload"});

Creating another controller

module.exports = function(router){
    return {
        name: "user.account.view",
        title: "Account / View Account",
        route: "/account",
        method: "GET",
        headers: null,
        middleware: ["app.authorization","user.authorization"],
        fn: function(req, res){
            res.send("View account");
        }
    };
};

Creating a middleware

module.exports = function(router){ return function(req, res, next){ //return res.send('App is not authorized'); //authorize your app next(); } };

//This part will be used for Postman module.exports.config = { name: "app.authorization", description: "Application Authorization", headers: [ { "key": "App-Access-Token", "value": "{{myAppName.app.token}}", "type": "text" } ] };

Creating another middleware

module.exports = function(router){
    return function(req, res, next){
        return res.status(401).send('User is not authorized');
        //authorize your user
        //next();
    }
};

module.exports.config = {
    name: "user.authorization",
    description: "User Authorization",
    headers: [
        {
            "key": "User-Access-Token",
            "value": "",
            "type": "text"
        }
    ]
};

Creating a custom middleware

module.exports = function(router){
    return {
        name: "user.login",
        title: "User / Login",
        route: "/login",
        method: "GET",
        headers: null,
        middleware: [
            "app.authorization",
            
            //CUSTOM MIDDLEWARE
            {
                name: "test.middleware",
                description: "Some description",
                fn: function(req, res, next){
                    console.log("custom middleware");
                    next();
                }
            }
        ],
        fn: function(req, res){
            res.send("user login");
        }
    };
};

Log routes (used in development)

console.log(router.collections.routes());

Output:

.-------------------------------------------------------------------------------------------------------------------------------------------.
|                                                                  ROUTES                                                                   |
|-------------------------------------------------------------------------------------------------------------------------------------------|
| Method |  Route   |              Middleware              |       Name        |      Description       |         Filename         | Active |
|--------|----------|--------------------------------------|-------------------|------------------------|--------------------------|--------|
| GET    | /        | app.authorization                    | app.dashboard     | Dashboard              | /index.js                | true   |
| GET    | /account | app.authorization,user.authorization | user.account.view | Account / View Account | /account/account.view.js | true   |
| GET    | /login   | app.authorization,test.middleware    | user.login        | User / Login           | /login.js                | true   |
'-------------------------------------------------------------------------------------------------------------------------------------------'

Log middlewares (used in development)

console.log(router.collections.routes());

Output:

.----------------------------------------------------------------------------------.
|                                   MIDDLEWARES                                    |
|----------------------------------------------------------------------------------|
|        Name        |        Description        |        Filename        | Custom |
|--------------------|---------------------------|------------------------|--------|
| app.authorization  | Application Authorization | /app.authorization.js  | false  |
| user.authorization | User Authorization        | /user.authorization.js | false  |
| test.middleware    | Some description          | {ROUTES}/login.js      | true   |
'----------------------------------------------------------------------------------'

Export to Postman

This will export two JSON files, one containing collection (postman_collection.js) and the other one containing the environment (postman_environment).

router.exportToPostman();

License

MIT