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

addandremoveroutes

v1.0.4

Published

This module deals with to add and remove routes on the fly to an existing API. It uses express-remove-route

Downloads

13

Readme

addandremoveroutes

This module deals with to add and remove routes on the fly to an existing API. It uses express-remove-route
If you need to do more complicate things as extend or override your API, you must use apiextender npm package.

NPMNPM

addandremoveroutes allow ta add and remove routes in a simple and fast mode and with few lines of code.

Installation

To use addandremoveroutes install it in your project by typing:

$ npm install addandremoveroutes

Using addandremoveroutes

This is an express module for nodejs and now we explain how to use it in you code to make your API modificable/updatable on the fly.

Include addandremoveroutes

Just require it like a simple package:

var addandremoveroutes = require('addandremoveroutes');

Using addandremoveroutes

var express=require('express'); 
var addandremoveroutes = require('addandremoveroutes');
var app=express();
var router = express.Router();

app.use('/foo', router);
   
// Add a route on the fly. Add Resource /foo/add/me in get method
addandremoveroutes.addRoute(router,"GET","/add/me",function(req,res){

req.staus(200).send({"content":"resource added on the fly"});
});
 
router.get("/removeme",function(req,res,next){
    // should never be reachable due the resource is removed on the fly in the next section
});

// Remove a route on the fly. Remove  resource "/foo/remove/me" in get method
addandremoveroutes.removeRoute(app, '/foo/remove/me');

Reference

addRoute(router,method,resource,expressLikeFunction)

This is the function that allow to add a resource on the fly The param router is the router or application(app) on which you want add the resource.
The param method is the method by which the resource is invoked
The param resource is the resource to add on the fly
The param expressLikeFunction is the express like function "function(req,res){"your logic"}" that define the new resource

Example:

ar express=require('express'); 
var addandremoveroutes = require('addandremoveroutes');
var app=express();


   
// Add a route on the fly. Add Resource /foo/add/me in get method
addandremoveroutes.addRoute(app,"GET","/foo/add/me",function(req,res){

req.staus(200).send({"content":"resource added on the fly"});
});

removeRoute(router,resource)

This is the function that allow to remove a resource on the fly
The param router is the router or application(app) on which you want remove the resource.
the param resource is the resource to remove on the fly

Example:

var express=require('express'); 
var addandremoveroutes = require('addandremoveroutes');
var app=express();
var router = express.Router();


router.get('/remove/me', function(res, res) {
    res.send('I should not be here');
});

app.use('/foo', router);

// Remove a route on the fly. Remove  resource "/foo/remove/me" in get method
addandremoveroutes.removeRoute(app, '/foo/remove/me');

Examples

Examples: How to make a dynamic API

From a shell, go in your application home directory and type de follow commands:

$ cd /Your_App_Home
$ npm install addandremoveroutes -S   // install addandremoveroutes

Now from your app.js include the addandremoveroutes module.

var express=require('express'); 
var addandremoveroutes = require('addandremoveroutes');
var app=express();

To make a dynamic API at runtime, we need to define an endpoint that wrap addandremoveroutes and add/remove resources ad runtime. To do it add the fee code lines to you app.js:

var express=require('express'); 
var addandremoveroutes = require('addandremoveroutes');
var app=express();
   
// .....
// Old app.js logic
// ..... 

// Define an endpoint that wrap "addandremoveroutes" to add resources on the fly
// The access to this endpoint should be protected with token privileges
app.post("/addresource",function(req,res,next){
    // check for tokens
    //.....
     
    addandremoveroutes.addRoute(app,req.body.method,req.body.resourcePath,req.body.ResourceLogicfunction);    
    res.send({"status":"add resource done"});
});

// Define an endpoint that wrap "addandremoveroutes" to remove resources on the fly
// The access to this endpoint should be protected with token privileges
app.post("/removeResource",function(req,res,next){
    // check for tokens
    //.....
     
    addandremoveroutes.removeRoute(app,req.body.resourcePath);    
    res.send({"status":"remove resource done"});
});

With no extension resource defined, if we test it with curl we have:

 $ curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/foo/add/resource
 HTTP/1.1 404 Not Found
 X-Powered-By: Express
 Content-Type: text/html; charset=utf-8
 Content-Length: 1946
 ETag: W/"JITdME9ePIMNamJKK4biwQ=="
 Date: Tue, 31 Jan 2017 16:14:41 GMT
 Connection: keep-alive

Now we extend endpoint with a new resource installed on the fly "/foo/add/resource" addandremoveroutes.addRoute(app,req.body.method,req.body.,req.body.);

EXTENDER='{
            "resourcePath":"/foo/add/resource",  // add a new endpoint
             "method":"GET",                  // add new endpoint in get method  
             "ResourceLogicfunction": function(req,res,next){   // this is the resource Function definition
                    
                        res.status.send({"content":"new resource added"});
             },
            
}
$
$ curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST http://hostname/addresource
    -d $EXTENDER
    
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 31
ETag: "35-6BXjKyRXlm+rSEU9a23z/g"
Date: Fri, 11 Nov 2016 13:16:44 GMT
Connection: keep-alive
 
{"status":"add resource done"} // new resource endpoint installed

now without shutdown and restart your application make the same test with curl. You can see that you API now responds on this resource:

$ curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/foo/add/resource
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 33
ETag: "35-6BXjKyRXlm+rSEU9a23z/g"
Date: Fri, 11 Nov 2016 13:16:44 GMT
Connection: keep-alive

 {"response":"new resource added"} // new resource response                                                           
$

now we remove again the resource just created

$ curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST http://hostname/removeResource
    -d $EXTENDER
    
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 34
ETag: "35-6BXjKyRXlm+rSEU9a23z/g"
Date: Fri, 11 Nov 2016 13:16:44 GMT
Connection: keep-alive
 
{"status":"remove resource done"} // new resource endpoint removed

now without shutdown and restart your application make the same test with curl. You can see that you API now responds with 404 on this resource:

 $ curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/foo/add/resource
 HTTP/1.1 404 Not Found
 X-Powered-By: Express
 Content-Type: text/html; charset=utf-8
 Content-Length: 1946
 ETag: W/"JITdME9ePIMNamJKK4biwQ=="
 Date: Tue, 31 Jan 2017 16:14:41 GMT
 Connection: keep-alive

License - "MIT License"

MIT License

Copyright (c) 2016 aromanino

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Author

CRS4 Microservice Core Team ([email protected])

Contributors

Alessandro Romanino ([email protected]) Guido Porruvecchio ([email protected])