我写了一个非常简单的本地模块来管理几行本地化文本.它使用节点require来加载语言文件,但我很可能遇到路径问题.我收到了Cannot find module错误.
.
+-- local_modules
| +-- lang
| | +-- package.json
| | +-- index.js
+-- locale
| +-- en.js
| +-- de.coffee
+-- init.js
+-- index.coffee
+-- package.json
Run Code Online (Sandbox Code Playgroud)
如若require如果尚未加载它的文件.
join = require('path').join;
_config.path = './locale';
lang = 'en';
_locales = {};
if(!_locales[lang]){
_locales[lang] = require(join(_config.path, lang));
}
Run Code Online (Sandbox Code Playgroud)
目录中的每个文件locale都是典型的Node.js模块,例如en.js:
module.exports = {
test: 'Hello World!'
};
Run Code Online (Sandbox Code Playgroud)
本地模块导出一个function(req, res, next){},用作Express中间件,并且应该将所需的对象附加到本地化的字符串上res.locals,但是,我看到了Cannot find module …