
Security News
GitHub Actions Checkout Now Blocks Risky pull_request_target Checkouts
GitHub Actions checkout now blocks risky pull_request_target checkouts by default to help prevent pwn request supply chain attacks.
express-based-controller
Advanced tools
It's a simple class for writting your ExpressJS controllers in the OOP-style.
const { Controller } = require('express-based-controller');
const keys = [
's3Alvx3JJsP',
'22AzxlevFs2',
'S2fs33kAj3l'
];
class MyConstroller extends Controller {
constructor() {
super();
this.actions = {};
this.actions.sayHello = {};
this.actions.sayHello.middlewares = [
(req, res, next) => {
req.message = req.query.name;
req.accessKey = req.query.key;
next();
}
];
this.actions.sayHello.accessControl = (req) => {
return keys.includes(req.accessKey);
};
// if you return object, it will be passed in res.json
// if you return string|number|boolean, ot will be passed in res.end
this.actions.sayHello.handler = (req, res) => {
return {
message: `Hello, ${req.message}!`
};
};
this.actions.sayHello.onError = (err, req, res) => {
return {
errors: true,
message: err.message
};
};
}
};
const myController = new MyController();
const actions = myController.compileActions();
// express app instance
const { app } = require('../app.js');
app.get('/hello', actions.sayHello);
import { Controller, action } from 'express-based-controller';
import { app } from '../app.ts';
const keys = [
's3Alvx3JJsP',
'22AzxlevFs2',
'S2fs33kAj3l'
];
function ExtractQuery(req, res, next) {
req.message = req.query.name;
req.accessKey = req.query.key;
next();
}
export class MyController extends Controller {
// You can use action method decorator
@action({
middlewares: [ExtractQuery],
accessControl: (req) => keys.includes(req.accessKey),
onError: (err, res, res) => {
return {
errors: true,
message: err.message
};
}
})
async sayHello(req, res) {
return {
message: `Hello, ${req.message}!`
};
}
};
const myController = new MyController();
const actions = myController.compileActions();
app.get('/hello', actions.sayHello);
You can use global middlewares, access control functions and error handlers:
import auth from '../middlewares/auth';
import { Controller, action } from 'express-based-controller';
const keys = [
's3Alvx3JJsP',
'22AzxlevFs2',
'S2fs33kAj3l'
];
const checkKeys = req => keys.includes(req.key);
class SomeController extends Controller {
public middleware = [auth];
public accessControl = checkKeys;
public onError = (err, req, res) => {
return {
errors: true,
message: err.message
};
};
@action()
sayHello(req, res, next) {
return {
message: `hello, ${req.query.name || 'anonymous'}!`
};
},
@action()
sayBye(req, res, next) {
return {
message: `bye, ${req.query.name || 'anonymous'}!`
}
}
}
After compilation auth, accessControl and onError will be called together with every action (sayHello, sayBye).
If you set the validator, req will be validated by Joi-schema:
local:
import { authenticate } from '../auth';
import { Controller } from 'express-based-controller';
class SomeController extends Controller {
@action({
validator: Joi.object({
body: Joi.object().keys({
username: Joi.string().required(),
password: Joi.string().required()
})
}).unknown(true),
onError: (err, req, res) => {
return {
errors: true,
message: err.message
};
}
})
async authenticate(req, res) {
if (authenticate(req.body)) {
return {
message: `Hello, ${req.body.username}!`
}
} else {
throw new Error('Wrong credentials!');
}
}
}
global:
import { Controller } from 'express-based-controller';
class SomeController extends Controller {
public validator = Joi.object({
body: Joi.object().keys({
message: Joi.string().required()
})
}).unknown(true),
@action()
sayHello(req, res, next) {
return {
message: `hello, ${req.body.message}`
};
},
}
By default, Joi-validator throws native Joi ValidationError:
export interface JoiObject {
isJoi: boolean;
}
export interface ValidationError extends Error, JoiObject {
details: ValidationErrorItem[];
annotate(): string;
_object: any;
}
But you can override joiValidationFormatter method:
import { ValidationError as JoiValidationError } from 'joi';
class SomeController extends Controller {
public joiValidationFormatter(error: JoiValidationError) :any {
error.name = "Request Validation Error!";
return error;
}
}
Also, you can change access control error:
import { ValidationError as JoiValidationError } from 'joi';
class SomeController extends Controller {
public joiValidationFormatter(error: JoiValidationError) :any {
error.name = "Request Validation Error!";
return error;
},
public accessControlException() :any {
const error = new Error('Access denied!');
error.name = 'Access Error';
return error;
}
}
compileActions public method compiles your actions, middlewares and access controll functions to a simple object, that has some middleware properties
For example:
import * as express from 'exoress';
import auth from '../middlewares/auth';
import { Controller, action } from 'express-based-controller';
const keys = [
's3Alvx3JJsP',
'22AzxlevFs2',
'S2fs33kAj3l'
];
class SomeController extends Controller {
public middlewares = [auth];
@action()
sayHello(req, res, next) {
return {
message: `hello, ${req.query.name || 'anonymous'}!`
};
},
@action()
sayBye(req, res, next) {
return {
message: `bye, ${req.query.name || 'anonymous'}!`
}
}
}
const compiledActions = new SomeController().compileActions();
const app = express();
app.listen(3000);
app.get('/hello', compiledActions.hello);
app.get('/bye', compiledActions.bye);
Middleware order into action after compilation:
import { authenticate } from '../auth';
import { Controller } from 'express-based-controller';
class AuthController extends Controller {
public middlewares = [authenticate];
public onError = (err, req, res, next) => {
return {
message: err.message
};
}
}
class SomeController extends AuthController {
@action()
sayHello(req, res, next) {
return {
message: `hello, ${req.query.name || 'anonymous'}!`
};
},
}
add middlewares:
import { authenticate } from '../auth';
import { Controller } from 'express-based-controller';
class AuthController extends Controller {
public middlewares = [authenticate];
public onError = (err, req, res, next) => {
return {
message: err.message
};
}
}
class SomeController extends AuthController {
constructor() {
super();
this.middlewares.push((req, res, next) => {
debug(req);
next();
});
}
@action()
sayHello(req, res, next) {
return {
message: `hello, ${req.query.name || 'anonymous'}!`
};
},
}
FAQs
OOP controller for ExpressJS with JoiValidators
The npm package express-based-controller receives a total of 0 weekly downloads. As such, express-based-controller popularity was classified as not popular.
We found that express-based-controller 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.

Security News
GitHub Actions checkout now blocks risky pull_request_target checkouts by default to help prevent pwn request supply chain attacks.

Product
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.

Product
Socket MCP now lets AI assistants review org alerts, investigate threats using the Socket threat feed, and inspect package files in addition to dependency scoring.