我正在研究一个非常大的节点项目,我想知道我当前要求文件然后在其中设置全局变量(可以在任何地方访问)的方法是一个好主意还是不安全。我想知道你们是否可以给我一些时间。
服务器.咖啡
#require and define all dependencies
require 'coffee-script'
GLOBAL.fs = require 'fs'
GLOBAL.crypto = require 'crypto'
GLOBAL.hound = require 'hound'
GLOBAL.async = require 'async'
require './config'
require './functions'
#define route
require './routes/index'
require './routes/login'
require './routes/editor'
#sockets
require './sockets/chat'
console.log "Listening"
配置.coffee
GLOBAL.express = require 'express'
GLOBAL.http = require 'http'
GLOBAL.ect = require 'ect'
GLOBAL.mysql = require 'mysql'
GLOBAL.sass = require 'connect-sass'
GLOBAL.coffeescript = require 'connect-coffee-script'
GLOBAL.SchemaObject = require 'node-schema-object'
GLOBAL.User = new SchemaObject
id: Number,
fullname: String,
secret: String,
email: String,
lastOpened: Number
GLOBAL.ectRenderer = ect
watch: true,
root: "#{__dirname}/views"
MemoryStore = express.session.MemoryStore
sessionStore = new MemoryStore()
GLOBAL.app = express()
app.configure ->
app.use(require('prerender-node'))
app.use(coffeescript(src: "#{__dirname}/public"))
app.use '/css/main.css', sass.serve("#{__dirname}/sass/main.scss")
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({secret: 'LOLLYU83rhf99fefru9d', store: sessionStore}));
app.use express.static("#{__dirname}/public")
GLOBAL.server = http.createServer(app).listen(8080)
GLOBAL.io = require('socket.io').listen(server)
GLOBAL.connection = mysql.createConnection
host: '.....',
user: '.....',
password: '.....',
database: '.....'
connection.connect()
有一个更好的方法吗。我觉得我只是猜测并编造了这个。我希望一切都超级整洁。和快。