Gathering detailed insights and metrics for express-association
Gathering detailed insights and metrics for express-association
Gathering detailed insights and metrics for express-association
Gathering detailed insights and metrics for express-association
A controller plugin designed to utilize mongoose with the mongoose-associations plugin
npm install express-association
Typescript
Module System
Node Version
NPM Version
JavaScript (53.39%)
TypeScript (46.61%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
1 Stars
28 Commits
1 Watchers
13 Branches
1 Contributors
Updated on Nov 15, 2019
Latest Version
0.0.22
Package Id
express-association@0.0.22
Unpacked Size
82.70 kB
Size
15.68 kB
File Count
48
NPM Version
6.9.0
Node Version
8.11.3
Cumulative downloads
Total Downloads
Designed To Increase Productivity and Organization while using Express
1npm install --save express-association
An app can have multiple routers, to create a router
1const { Router } = require('express-associaiton') 2const router = new Router(router => { 3 router.get('test', { as: 'Api#get' }) // resolves to GET on /test 4}) 5router.post('test', { as: 'Api#post' }) // resolves to POST on /test
To configure the router, you may either use the configuration block or take the instance and call directly on it. The first style is preferred because it show nested routing with indentation.
By default routers do not have an namespace. To create a name space for an router:
1const router = new Router(router => { 2 router.namespace('super_namespace/v76') 3 router.get('test', { as: 'Api#get' }) // resolves to GET on /super_namespace/v76/test 4})
To create nested routes:
1const router = new Router(router => { 2 router.get('test', { as: 'Api#get' }) // resolves to GET on /test 3 router.route('nested', route => { 4 route.get('test', { as: 'Api#get' }) // resolves to GET on /nested/test 5 }) 6})
get
, post
, put
, delete
are the 4 HTTP protocols currently supported by express-association they can be invoked by:
1const router = new Router(router => { 2 router.get('get', { as: 'Api#get' }) // resolves to GET on /test 3 router.post('post', { controller: 'Api', action: 'post' }) // resolves to POST on /post 4 router.put('put', { as: 'Api#put' }) // resolves to POST on /put 5 router.delete('delete', { as: 'Api#delete' }) // resolves to POST on /delete 6})
Each of these resulting route is pointed at an action
by its second parameter as
property in the format of Controller#Action
an alternative method is to specify property controller
, action
for the route.
A resource is a special route that is designed around a restful resource. A resource create a one to one relationship with a controller
1const router = new Router(router => { 2 router.resource('User', resource => { 3 resource.collection.get('test') // resolves to GET on /users/test 4 resource.collection.get('another_test', { action: 'test' }) // resolves to GET on /users/another_test 5 resource.member.get('test') // resolves to GET on /users/:userId/test 6 }) 7})
when creating a resource, you may specify the controller that the resource logic will be implemented as in this case User
. A resource will automatically create 2 properties - collection
and member
. The collection is a route that will automatically generate the namespace for the defined controller name in the plural, snake case, lowercase form. e.g. users
for the resource User
. The member is another route that will include which include also the id url parameter for the resource in the singular, camel case, with Id format.
A resource will also automatically generate 5 actions that are equivalent to:
index
1resource.collection.get('', { as: 'User#index' }) // resolves to GET on /users
show
1resource.member.get('', { as: 'User#show' }) // resolves to GET on /users/:userId
create
1resource.collection.post('', { as: 'User#create' }) // resolves to POST on /users/
update
1resource.member.put('', { as: 'User#update' }) // resolves to PUT on /users/:userId
delete
1resource.member.delete('', { as: 'User#delete' }) // resolves to DELETE on /users/:userId
To not generate these routes or only a few of them you can do:
1const router = new Router(router => { 2 router.resource('User', resource => { 3 resource.only('index', 'show') 4 }) 5})
This will restrict the automatically created actions to only index and show.
Controllers are used to group related logic as actions. Controllers are extended from the base class Controller.
1const { Controller } = require('express-association') 2 3class UserController extend Controller { 4 5}
To learn more about how to name and export your controller, please review ClassFinder of node-associaiton
Are instance function on the controller. Each route is mapped to one of these.
an action can be either async or sync function without any arguments.
During an request, express association will create an instance of the controller. When performing the action, this instance is the the scope this
. You can access the request, and response from this. e.g.
1class UserController extend Controller { 2 async action() { 3 this.send({ 4 request: this.request, 5 response: this.response 6 }) 7 } 8}
The above action performs the User#action
method. express-association has many express function mapping for the request and response, e.g. send
to simplify middleware creation. this example sends the request and response out as json.
The action is where the meat / main logic of an request should be implemented. Auxiliary middleware for permission verification, An random express middleware plugin that you have found on the internet, etc are set up as a before function.
1const someBeforeMiddlewareFunction = (request, response, next) => { 2 console.log('I am a middleware!') 3 next() 4} 5UserController.before(someBeforeMiddlewareFunction)
To scope the middleware functions to specific actions on the controller in two ways:
1UserController.before(someBeforeMiddlewareFunction, { 2 only: ['index'] 3})
Only specify that this middleware will only
be used before the declared actions.
1UserController.before(someBeforeMiddlewareFunction, { 2 only: ['index'] 3})
Except will do the opposite and run the middleware on all actions except
the declared actions.
After middleware are design for processing error handling. you can define a 4 argument express middleware:
const someAfterMiddlewareFunction = (error, request, response, next) => {
console.log('I am a middleware!')
next(error)
}
UserController.after(someAfterMiddlewareFunction)
After middleware can also be specified on the controller as a instance function similar to actions. For these functions the error can be accessed via this.error
1class UserController extend Controller { 2 async someAfterMiddleware() { 3 console.log(this.error) 4 throw this.error 5 } 6}
Unless the error is handled, an after controller as instance method should throw the error again.
The scoping of after middleware can be done using only
and except
in the same way as before middleware
Parameter validation for express-association is implemented using JOI
1const Joi = require('joi') 2UserController.parameter('firstName', Joi.string().required(), { 3 only: ['index'] 4})
express-association combines all query and body request parameters for validation. and can scope to specific action similar to before middleware
Many types of errors can be generated by a request. All unhandled errors will be converted to an UncaughtError by express-association with the default status of 500. All parameter validation will be converted to an ParameterValidationError with the default status of 422. To create custom errors
1const { ApplicationError } = require('express-association') 2class IAmATeaPotError extend ApplicationError { 3 constructor(message = 'Something went wrong', log = 'I am a tea pot') { 4 super(message, log) 5 } 6 static get status() { 7 return 418 8 } 9} 10UserController.error(IAmATeaPotError)
Based on the above scenario, all Thrown IAmATeaPotError
will be handled. It will log to console 'I am a team pot' and return 'something went wrong as the message' as an response to the request.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
Found 0/28 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
license file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
42 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreLast Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year