Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8538007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:02:30+00:00 2026-06-11T11:02:30+00:00

I have an Expressjs app running on NodeJS 0.8.8 using MongoDB and the Jade

  • 0

I have an Expressjs app running on NodeJS 0.8.8 using MongoDB and the Jade template language, and I’d like to allow the user to configure many of the site-wide presentation options such as the page titles, logo image, etc.

How can I store these configuration options in a mongoDB database so that I can read them upon application startup, manipulate them while the app is running, and display them in the jade templates?

Here’s my general app 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 function called getSiteConfig in accessDB.js that runs Site.find()… to retrieve the fields in the one document in the collection.

So this is the crux of the question: how should I inject those fields into the express app so that they can be used throughout the site? Should I follow the same pattern I used with the moment.js tool? Like this:

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

If not, what would be the correct way to do this?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:02:32+00:00Added an answer on June 11, 2026 at 11:02 am

    Consider using express middleware for loading site config.

    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();
        });
      });
      ...
    });
    

    Throwing an err is a realy bad idea because it will crash your application.
    So use next(err); instead. It will pass your error to express errorHandler.

    If you already authenticated your user (in previous middleware, for example) and stored its data into req.user, you can use it to get the right config from db.

    But be careful with using your getSiteConfig function inside of express middleware because it will pause express from further processing of the request until the data is received.

    You shall consider caching siteConfig in express session to speedup you application. Storing session-specific data in express session is absolutely safe because there is no way for user to get access to it.

    The following code demonstrates the idea of caching siteConfig in 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();
        });
      });
      ...
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a basic expressjs app (using jade), but I am having trouble rendering
I'm using Expressjs w/ node.js and I have a route like this: users.init(app.db, function()
I'm starting to experiment with nodejs/expressjs/coffeescript and the jade view engine. I have the
I have a Node.js app using Express that stores data in a mongoDB (locally).
I have two Express.js apps running on my server. A plain vanilla app called
I have an expressjs app with everyauth setup. Everyauth is working fine. Now, I
I have the following express node.js app. It's using the 'redis' npm package. app.get(/test,function(req,res){
I have a JavaScript data structure like the following in my Node.js/Express web app:
I am developing a Silverlight app using VS2008 Express. I have just implemented a
I have an app running with: one instance of nginx as the frontend (serving

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.