Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to place common functions in express.js?

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

Is there any "best practice" for it? Nothing is mention in the documentation about it.

like image 290
Alvaro Avatar asked Sep 25 '14 11:09

Alvaro


1 Answers

They should be placed in an 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();
like image 103
Kevin Reilly Avatar answered Oct 13 '22 02:10

Kevin Reilly