node.js - express app correct way to connect to mongo db? -


i have following directory layout contains node.js / express application:

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 

the /src/config/index.coffee has details of mongo url export 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 dexies database on mongodb boundservices = if process.env.vcap_services json.parse(process.env.vcap_services) else null unless boundservices     if db_user , 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 current environment true in 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 

and in /src/utils/dbconnect.coffee, have following:

# connecting 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 =   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 database: " + db_url, logcategory  # check , connect redis  exports = module.exports = db_connect_mongo 

am correct in assuming once start application, /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 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 

the connection stays open, don't have keep opening , closing this?

is correct approach this?

any advice appreciated.

yes, once connect, connection stay open until explicitly disconnected or goes idle while. docs:

for long running applictions prudent enable keepalive. without it, after period of time may start see "connection closed" errors seems no reason. if so, after reading this, may decide enable keepalive:

your db_connect_mongo violates node convention first argument null when invoking callback in success case. correct this, don't use callback(true, db) mean success, use callback(null, db). other , being bit complicated can , should simpler, have should work.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -