简体   繁体   English

在express.js中放置常用功能的位置?

[英]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. 关于它的文档中没有提到任何内容。

They should be placed in an include that you require from each route. 它们应该放在每条路线require的包含中。

common.js common.js

function Common(){}

Common.prototype.method1 = function(){}
Common.prototype.method2 = function(){}

module.exports = new Common();

route.js route.js

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM