我對nodejs相當陌生。編寫我的第一份申請。我很習慣於PHP。在NodeJS模塊中使用模塊
爲了保持代碼的組織和清潔,我總是在不同的文件中編寫函數,並將它們包含在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沒有定義的錯誤。
我該如何解決這個問題?
的可能的複製[Node.js的 「需要」 的功能和參數(HTTPS ://stackoverflow.com/questions/7367850/node-js-require-function-and-parameters) –