JavaScript/Nodejs를 처음 사용했습니다. Nodejs 애플리케이션에서 구성을 공유하려면 어떻게해야합니까? 예를 들면 : 나는 config/config.coffee
Nodejs 응용 프로그램에서 구성 변수를 공유하는 방법
path = require("path")
module.exports = {
development:
db: 'mongodb://localhost/hello'
root: rootPath = path.normalize(__dirname + '/..')
}
내가 내 app.coffee
에서 config.coffee
포함되어 있습니다.
express = require("express")
# Load configurations
env = process.env.NODE_ENV || 'development'
config = require("./config/config")[env]
require('./config/boot')
app = express()
지금 내 config/boot.coffee
에 config
변수를 포함합니다. 내가 어떻게 해? config/boot.coffee
에 config/config.coffee
을 다시 포함하고 싶지 않습니다. 여기에 내 config/boot.coffee
파일입니다. 나쁜 영어
env = process.env.NODE_ENV || 'development'
config = require("./config")[env]
fs = require("fs")
mongo = require("mongoose")
# Bootstrap db connections
mongo.connect config.db
# Bootstrap models
models_path = config.root+"/app/models"
fs.readdirSync(models_path).forEach((file)->
require(models_path + '/' + file) if ~file.indexOf('.coffee')
)
# Bootstrap services
services_path = config.root+"/app/services"
fs.readdirSync(services_path).forEach((file)->
require(models_path + '/' + file) if ~file.indexOf('_service.coffee')
)
죄송합니다 :(
멋지 네요, 당신의 블로그에 북마크 :) – Zeck