
Product
Introducing Custom Tabs for Org Alerts
Create and share saved alert views with custom tabs on the org alerts page, making it easier for teams to return to consistent, named filter sets.
Flexible controllers for express.js apps.
Easily load your controllers, no matter where they are, and mount their actions/routes to your app.
npm install exctrl
File app.js:
var express = require('express'),
app = express(),
exctrl = require('exctrl');
// configuring express app...
exctrl.load(app, {pattern: __dirname + '/controllers/*.js'});
File /controllers/user.js:
exports.create = function (req, res) {
// ...
res.send('user created');
};
exports.read = function (req, res) {
// ...
res.send('user read');
};
exports.update = function (req, res) {
// ...
res.send('user updated');
};
exports.del = function (req, res) {
// ...
res.send('user deleted');
};
exports.index = function (req, res) {
// ...
res.send('all users');
};
Results in:
POST /user
GET /user/:id
PUT /user/:id
DELETE /user/:id
GET /user
An URL generated by exctrl has the format: [/<prefix>]/<controller name>[/<method name>][/<...>]
<prefix>An optional prefix to add to all mounted routes, and is provided via the options hash, see the load function below.
<controller name>The main mount point for each controller and can be provided in three different ways:
load function (see options hash for options on how to extract the name)name property of the controller if it's a stringmount function, see below<method name>Some method names are considered "magic", in that way that it may imply both HTTP method and URL:
index, list or search
GET<method name> is left empty), e.g. "/user"create
POSTread
GET:id"/<controller name>/:id"update
PUT:id"/<controller name>/:id"del
DELETE:id"/<controller name>/:id"If a method name starts with a known HTTP method, i.e. get, head, post, put and delete, it is mounted as that method in the provided express.js app, otherwise it's considered a get method.
The HTTP method part from the method name is then cut off, and the remainings are dasherized (e.g. "getBestFriends" -> "best-friends") and added to the url.
Examples
GET /user/best-friendsGET /products/top-sellersPOST /products/likeGET /user/:id/friends<...> is for..A controller action mustn't be just a function, e.g:
var basicController = {
name: 'basic',
getStuff: function (req, res) {
// ROUTE: GET /basic/stuff
}
};
For more advanced usage you can declare a controller action as an array (like AngularJS dependency injection syntax), e.g:
var middleware = function (req, res, next) { /* ... */ };
var advController = {
name: 'adv',
getStuff: [':type', middleware, ':anotherparam', 'chunk', function (req, res) {
// ROUTE: GET /adv/stuff/:type/:anotherparam/chunk
if (req.params.type === 'other') {
this.otherStuff(req, res); // Yes, `this` here is the actual controller
} else {
/* do something else */
}
}],
otherStuff: function (req, res) {
// ROUTE: GET /adv/other-stuff
res.send(/* other stuff */);
}
}
app - Your express.js appoptions - A hash of options
pattern
"controller/*.js" or "**/*.ctrl.js"nameRegExp
"**/*.ctrl.js" a nameRegExp as /([^\/\\]+).ctrl.js$/ can be used to extract "user" from /var/tmp/dir/user.ctrl.js. The first match group will be used as extractor, and full path filename will be used when matching.prefix
api, controller name user and action/method get the mounted route will be: GET /api/userload() loads your controllers, and does so syncronously to make sure the routes are loaded at the time you want, to not interfere with other express.js configurations.
app - Your express.js appbind() binds your app to exctrl, so that you can skip first param to the load function above.
exctrl.bind(app)
.load(options);
name - The controller name, used as mount pointcontroller - A controller with methods as actionsmount() is mainly used internally by exctrl for mounting a controller at a mount point, but is available publicly for convenience, and will stay in the API.
Note if the controller object has a name property with type String the first parameter to this function can be omitted, and the controller.name will be used as mount point instead.
var name = 'user',
controller = {
index: function (req, res) {
res.send(/* all users */);
}
};
exctrl.bind(app)
.mount(name, controller);
// or
controller.name = name;
exctrl.bind(app)
.mount(controller);
exctrl uses Semantic Versioning as versioning model.
Please feel free to contribute and send pull requests!
MIT, see ./LICENSE file.
FAQs
Flexible controllers for express.js apps
The npm package exctrl receives a total of 0 weekly downloads. As such, exctrl popularity was classified as not popular.
We found that exctrl demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Product
Create and share saved alert views with custom tabs on the org alerts page, making it easier for teams to return to consistent, named filter sets.

Product
Socket’s Rust and Cargo support is now generally available, providing dependency analysis and supply chain visibility for Rust projects.

Security News
Chrome 144 introduces the Temporal API, a modern approach to date and time handling designed to fix long-standing issues with JavaScript’s Date object.