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

glue-controller

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

glue-controller

Simple controller (MVC pattern) for Node.js that works with Express or Koa

  • 2.0.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
4
decreased by-77.78%
Maintainers
1
Weekly downloads
 
Created
Source

glue-controller

npm version Licence Github issues Github stars

Goal

The goal of this projet is to deliver a Controller component, for a Node.js environemt, that can be used in a similar way as to a classic MVC framework.

Note: The package supports both express and koa libraries.

Install

npm i glue-controller --save-prod

Basic usage and setup

import Controller from 'glue-controller';
import express from 'express';

class ProductController extends Controller {

  static routesMap = {
    actionCreate: [{ method: 'POST' }]
  };

  actionList(req, res) {
    res.send('Response for "GET /product/list"');
  }

  actionCreate(req, res) {
    res.send('Response for "POST /product/create"');
  }
}

// setup with express
const app = express();
const router = express.Router();

new ProductsController({ router });

app.use(router);
app.listen(3000);

As you can see the above example, the routes path are automatically set by the library based on the given prefix (if setted), the controller name and followed by the action name. This feature that is present in a lot of MVC frameworks out there and that keep the application easy to follow through and less verbose. It's possible to change this behavior as explained bellow.

Advanced usage

class SiteController extends Controller {

  // middlewares to apply to all actions in the controller
  static middlewares = [authenticationMiddleware];

  // routing configuration
  static get routesMap() {
		return {
      actionProducts: [
        { method: 'GET' },
        { method: 'POST', delegate: 'createProduct' },
        {
          method: 'PATCH',
          delegate: 'updateProduct',
          path: `${this.prefix}/product/:id`
        }
      ],
      deleteProduct: [
        {
          method: 'DELETE',
          middlewares: [beforeDeleteMidleware],
          path: `${this.prefix}/product/:id`
        }
      ]
    };
  }

  static get prefix() {
		return '/site';
	}

  actionProducts(req, res) {
    res.send('Response for "GET /products"');
  }

  createProduct(req, res) {
    res.send('Response for "POST /site/product"');
  }

  updateProduct(req, res) {
    res.send(`Response for "PATCH /site/product/:id"`);
  }

  deleteProduct() {
    res.send('Response for "POST /site/product/:id"');
  }
}

As mentioned before, any method declared in the Controller class, that starts with action followed by an uppercase character, eg.: actionProducts, will be assumed as an action and a new route will be automatically set but it's also possible to change certain behaviors.

You can make use of the static property routesMap (object type) to configure existing actions or to create new ones. It's importante to note that all the keys must directly match a specific method name declared in the class.

As demonstrated in the example above you can mention directly a declared method that doesn't start with action. The only difference is that in a non-action name, eg.: deleteProduct the route is not automatically set for you, so you always need to set one. None of the approaches is better than other and it's encouraged to interchange the use as needed.

Controller properties

NameTypeVisibilityDescription
routesMapobjectpublic staticRoutes configuration. All keys must directlly reference a method declared in the controller (more details bellow).
middlewaresarraypublic staticList of middlewares that will run in every action defined in the controller.
getControllerPathfunctionpublicreturns the partial controller path (Controller name in kebab-case).
getRouterfunctionpublicreturns the router object (express or koa router).
getPrefixfunctionpublicreturns the prefix set when the controller instance was created.
getControllerNamefunctionpublicreturns the raw controller name.

Static property routesMap

NameTypeDescription
methodstringHTTP method/verb
delegatestringMethod to delegate the action execution. This method should be declared in the controller or should exist in it's prototype chain.
routestringThe route that is passed over to the express or koa router.
middlewaresarrayList of middlewares to apply to a specific action.

Contributions

Contributions are very welcome. There's a lot of room for improvements and new features so feel free to fork the repo and get into it. Also, let me know of any bugs you come across, any help on bug fixing is also a plus!

Keywords

FAQs

Package last updated on 15 Aug 2019

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