【发布时间】:2017-10-27 03:06:41
【问题描述】:
我对 nodejs 还很陌生。编写我的第一个应用程序。我很习惯 php。
为了保持代码的组织和整洁,我总是在单独的文件中编写函数,并根据需要将它们包含在 php 中。
但是,在 nodejs 中,我必须像需要模块一样需要它们。 例如。
functions.js
module.exports = {
check_db : function(key){
},
check_cache : function(key){
memcached.get(key,function(err, data){
console.log(data);
});
},
};
像这样包含在主应用程序中
// Establish connection with cache and database
const mysql = require('mysql2');
const Memcached = require('memcached');
const memcached = new Memcached('localhost:11211');
const bb = require('bot-brother');
//Load the database cache functions
const dbc = require("./functions");
dbc.check_cache(123);
现在我可以从主应用程序文件中访问 dbc 中的函数,但我无法从函数文件中使用主应用程序中所需的模块。 我收到未定义 memcached 的错误。
我该如何解决这个问题?
【问题讨论】:
标签: javascript node.js module memcached require