How to save site configuration in MongoDB for NodeJS Express application?

I have an Expressjs application running on NodeJS 0.8.8 using MongoDB and the Jade template language, and I would like to allow the user to configure many presentation parameters on the site, such as page names, logo, etc.

How to save these configuration parameters in the mongoDB database so that I can read them when the application starts, manage them while the application is running and display them in jade templates?

Here is my general application setup:

var app = module.exports = express();
global.app = app;
var DB = require('./accessDB');
var conn = 'mongodb://localhost/dbname';
var db;

// App Config
app.configure(function(){
   ...
});

db = new DB.startup(conn);

//env specific config
app.configure('development', function(){
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
}); // etc

// use date manipulation tool moment
app.locals.moment = moment;

// Load the router
require('./routes')(app);

So far I have created a model called "Site" for the "siteConfig" collection, and I have a getSiteConfig function in accessDB.js that runs Site.find () ... to get the fields in one document in the Collection.

, : -, ? , moment.js? :

db.getSiteConfig(function(err, siteConfig){
  if (err) {throw err;}
  app.locals.siteConfig = siteConfig;
});

, ?

!

+5
1

.

app.configure(function() {
  app.use(function(req, res, next) {
    // feel free to use req to store any user-specific data
    return db.getSiteConfig(req.user, function(err, siteConfig) {
      if (err) return next(err);
      res.local('siteConfig', siteConfig);
      return next();
    });
  });
  ...
});

- , . next(err);. , errorHandler.

(, ) req.user, db.

getSiteConfig , .

siteConfig -, . , -, , .

siteConfig express sessionn:

app.configure(function() {
  app.use(express.session({
    secret: "your sercret"
  }));
  app.use(/* Some middleware that handles authentication */);
  app.use(function(req, res, next) {
    if (req.session.siteConfig) {
      res.local('siteConfig', req.session.siteConfig);
      return next();
    }
    return db.getSiteConfig(req.user, function(err, siteConfig) {
      if (err) return next(err);
      req.session.siteConfig = siteConfig;
      res.local('siteConfig', siteConfig);
      return next();
    });
  });
  ...
});
+16

All Articles