Where to place common functions in express.js?

I am wondering where common functions should be placed in the express structure for sharing between different routes.

Is there any "best practice" for this? Nothing is mentioned in the documentation about this.

+5
source share
1 answer

They should be placed in the include that you require from each route.

common.js

 function Common(){} Common.prototype.method1 = function(){} Common.prototype.method2 = function(){} module.exports = new Common(); 

route.js

 var common = require('./common'); common.method1(); common.method2(); 
+11
source

Source: https://habr.com/ru/post/1203387/


All Articles