我想将node.js文件拆分为多个文件,如:
我关心的是这是一种普遍的还是良好的做法。
以下文件分割后:
app.js
require('./restful');
require('./database');
config.js
module.exports = function(){
database :{
username: "mark",
password: "pass",
host: "127.0.0.1",
port: "27017",
name: "mydb"
};
database.js
var config = require('./config');
var mongodb = require('mongodb');
var url = 'mongodb://'+ config.database.username+ ':' + config.database.password + '@' + config.database.host + ':' + config.database.port + '/' + config.database.name;
var Database = function() {
var db;
// always return the singleton instance, if it has been initialised once already.
if (Database.prototype._singletonInstance) {
return Database.prototype._singletonInstance;
}
this.getDatabase = function() {
return db;
}
this.get = this.getDatabase;
mongodb.MongoClient.connect(url, function(err, result) {
if(err || result === undefined || result === null) {
throw err;
} else {
db = result;
}
});
Database.prototype._singletonInstance = this;
};
new Database();
console.log('MongoDb set up OK!');
exports.Database = Database;
};
restful.js
var express = require('express');
var bodyParser = require('body-parser');
var restful_express = express();
restful_express.use(bodyParser());
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, POST');
res.header('Access-Control-Expose-Headers', 'token');
res.header('Access-Control-Allow-Headers', 'Content-Type, X-XSRF-TOKEN, Last-Event-ID, Content-Language, Accept-Language, Accept');
res.header('X-XSS-Protection', 0);
next();
}
restful_express.use(allowCrossDomain);
restful_express.listen(process.env.PORT || 7272, function(err) {
winlog.info('ExpressJs listening at 7272');
});
var Restful = function() {
// always return the singleton instance, if it has been initialised once already.
if (Restful.prototype._singletonInstance) {
return Restful.prototype._singletonInstance;
}
this.getRestful = function() {
return restful_express;
}
Restful.prototype._singletonInstance = this;
};
new Restful();
console.log('Express.js Restful set up OK!');
exports.Restful = Restful;
user.js的
var config = require('./config');
var database = require('./database');
var db = database.Database();
var restful = require('./restful').Restful().getRestful();
module.exports = function(){
restful.get('/getUser/:email', function(req, res) {
db.get().collection("user").findOne({email:req.param('email')}, function(err, result) {
if(result) {
res.send({name:result.name});
} else {
res.statusCode = 400;
res.send({msg:'Email not found'});
}
});
});
});
group.js
var config = require('./config');
var database = require('./database');
var db = database.Database();
var restful = require('./restful').Restful().getRestful();
module.exports = function(){
restful.get('/getGroup/:name', function(req, res) {
db.get().collection("group").findOne({name:req.param('name')}, function(err, result) {
if(result) {
res.send({name:result.name});
} else {
res.statusCode = 400;
res.send({msg:'Group not found'});
}
});
});
});
所以database.js可以简化为:
var config = require('./config');
var mongodb = require('mongodb');
var db;
var url = 'mongodb://'+ config.database.username+ ':' + config.database.password + '@' + config.database.host + ':' + config.database.port + '/' + config.database.name;
module.exports = function(){
initDb : function () {
mongodb.MongoClient.connect(url, function(err, result) {
if(err || result === undefined || result === null) {
throw err;
} else {
db = result;
}
});
},
getDb : function () {
if(db === null || db === undefined) {
this.initDb();
}
return db;
}
};
答案 0 :(得分:0)
这只是一种绝对合法的方式。但是相当复杂。
module
默认为单身。无论您拨打require('some_module');
多少次,它的脚手架代码只执行一次。在REPL中尝试以下代码
//index.js
var i = 0;
i ++;
exports.show = function(){
return i;
};
exports.increment = function(){
return i++;
};
然后在REPL中
> var one = require('./index.js');
> one.show();
1
> var two = require('./index.js');
> two.show();
1
> one.increment();
2
> two.show();
2