0
我有一个包含的Node.js/Express应用程序下面的目录布局:表示应用程序连接到mongo db的正确方法?
server.coffee
server.js
src
├── config
│ └── index.coffee
├── controllers
│ ├── index.coffee
│ └── user.coffee
├── index.coffee
├── models
│ └── user
│ └── user.coffee
├── routes.coffee
└── utils
├── dbconnect.coffee
views
├── 404.blade
├── 500.blade
├── index.blade
└── user
├── create.blade
的/src/config/index.coffee有蒙戈URL这是细节我再导出为DB_URL
#### Config file
# Sets application config parameters depending on `env` name
logger = require "../utils/logger"
logCategory = "Server config"
DB_HOST = "localhost"
DB_PORT = "27017"
DB_NAME = "zmgc"
DB_URL = null
DB_USER = null
DB_PASS = null
# Connecting to dexies database on mongodb
boundServices = if process.env.VCAP_SERVICES then JSON.parse(process.env.VCAP_SERVICES) else null
unless boundServices
if DB_USER and DB_PASS
DB_URL = "mongodb://#{DB_USER}:#{DB_PASS}@#{DB_HOST}:#{DB_PORT}/#{DB_NAME}"
else
DB_URL = "mongodb://#{DB_HOST}:#{DB_PORT}/#{DB_NAME}"
else
service_type = "mongodb-1.8"
credentials = boundServices["mongodb-1.8"][0]["credentials"]
DB_URL = "mongodb://" + credentials["username"] + ":" + credentials["password"] + "@" + credentials["hostname"] + ":" + credentials["port"] + "/" + credentials["db"]
#Set the current environment to true in the env object
exports.setEnvironment = (env) ->
logger.info "Set app environment: #{env}", logCategory
switch(env)
when "development"
exports.DEBUG_LOG = true
exports.DEBUG_WARN = true
exports.DEBUG_ERROR = true
exports.DEBUG_CLIENT = true
exports.DB_URL = DB_URL
when "testing"
exports.DEBUG_LOG = true
exports.DEBUG_WARN = true
exports.DEBUG_ERROR = true
exports.DEBUG_CLIENT = true
exports.DB_URL = DB_URL
when "staging"
exports.DEBUG_LOG = true
exports.DEBUG_WARN = true
exports.DEBUG_ERROR = true
exports.DEBUG_CLIENT = true
exports.DB_URL = DB_URL
when "production"
exports.DEBUG_LOG = false
exports.DEBUG_WARN = false
exports.DEBUG_ERROR = true
exports.DEBUG_CLIENT = false
exports.DB_URL = DB_URL
else
logger.info "Environment #{env} not found", logCategory
,然后在/src/utils/dbconnect.coffee,我有以下几点:
# Connecting to database on mongodb
config = require "../config/index"
logger = require("./logger")
mongoose = require("mongoose")
mongoose.set "debug", true
logCategory = "DATABASE Connection"
db_connect_mongo = init: (callback) ->
self = this
mongo_options = db:
safe: true
db_url = config.DB_URL
mongoose.connect db_url, mongo_options
db = self.db_mongo = mongoose.connection
db.on "error", (error) ->
logger.error "ERROR connecting to: " + db_url, logCategory
callback error, null
db.on "connected", ->
logger.info "SUCCESSFULLY connected to: " + db_url, logCategory
callback true, db
db.on "disconnected", ->
logger.info "DISCONNECTED from the database: " + db_url, logCategory
# check and connect to Redis
exports = module.exports = db_connect_mongo
我在假设正确,一旦我开始申请,/src/index.coffee
:
express = require "express"
logger = require "./utils/logger"
# Initialize logger
logger.configure()
#### Application initialization
# Create app instance.
app = express()
# Define Port
app.port = process.env.PORT or process.env.VMC_APP_PORT or process.env.VCAP_APP_PORT or 3000
# Config module exports has `setEnvironment` function that sets app settings depending on environment.
config = require "./config"
logCategory = "Server"
app.configure "development", "testing", "staging", "production", ->
config.setEnvironment app.settings.env
# Database connection
dbconnection = require "./utils/dbconnect"
dbconnection.init (result) ->
if result
logger.info "Database initialized", logCategory
连接保持打开状态,让我没有继续打开和关闭呢?
这是正确的做法吗?
任何意见非常赞赏。
它不起作用,在我的情况下,如果我关闭mongo,并尝试通过应用程序保存某些内容,猫鼬会记录存在正在保存的内容,但就是这样!我想能够有一个按摩来说'数据库没有连接'...我如何纠正节点约定,你会如何使这更简单?谢谢 – khinester 2013-05-21 14:42:39
“你会如何简化这个”稍微超出了stackoverflow的格式,但只要看看你有多少代码就像那个90%无用的巨大switch语句。 Mongoose可能会在数据库关闭的同时排队命令,这是部分帮助和部分烦人的。目前还不清楚如何尽可能地禁用该功能,但也许你可以调入'mongoose.connection'对象。 – 2013-05-21 14:54:49
好的,但对于'db_connect_mongo'违反,你是什么意思? – khinester 2013-05-21 15:17:58