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 6540873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:02:03+00:00 2026-05-25T11:02:03+00:00

when I load a static html page I want to send a JS Object

  • 0

when I load a static html page I want to send a JS Object to that page (which is data that comes from mongo). I can accomplish this with a Socket emit, but that seems like overkill. I also know I can write the whole document using JS, but I want to leverage HTML for the bulk of the page as it makes things easier for people I work with.

It would be nice if I could inject html into my static page before it gets sent out by the server, but I don’t think this is possible. I’ll settle with an html page that has either a function to parse data sent to the page at “load” time, etc..

My current samples are bogus or I’d send them along. Thanks for looking at my
question!

** current snippet, for fun **

function createServer(req, res) {
    var path = url.parse(req.url).pathname;
    var fsCallback = function(error, data, cb) {
        if(error) throw error;

        res.writeHead(200);
        res.write(data);
        res.end();

        if(cb) cb();
    }

    switch(path) {
        case '/subpage':
        case '/subpage.html':
            doc = fs.readFile(__dirname + '/subpage.html', function(error, data) {
                fsCallback(error, data, function() {
                    var db = new mongo.Db('mydb', new mongo.Server('localhost', '27017', {}), {});
                    db.open(function() {
                        db.collection('mytable', function(error, collection) {
                            collection.find(function (error, cursor) {
                                //store some data and send it to the page -- that will happen somewhere around here?
                            });
                        });
                    });
                });
            });
        break;
        default:
            doc = fs.readFile(__dirname + '/index.html', fsCallback);
        break;
    }
}
  • 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-05-25T11:02:04+00:00Added an answer on May 25, 2026 at 11:02 am

    I’ve had great results with Express and the JADE templating engine.

    I find that JADE makes hand-crafted HTML much cleaner and less bulky, even if most of the page is static. And, inserting dynamic data is obviously supported. JADE is simple and elegant with a very straightforward mapping between template syntax and generated HTML. With the indentation driving hierarchy versus more fragile XML tags, and minus the verbosity of all the closing tags, I have found JADE to be quicker to write and significantly more maintainable.

    For example

    html
      body
        div.myClass
         img(src="images/foo.png")
    

    versus

    
    <html>
      <body>
        <div class="myClass">
          <img src="images/foo.png"/>
        </div>
      </body>
    </html>
    

    if you really want to keep your existing static HTML file, narrowly solving your asked problem is pretty easy. There’s absolutely no reason you need to spit out the HTML file before the DB data comes back (unless you deliberately want the data populated async to mask DB latency).

    Transforming your existing code sample:

    function createServer(req, res) {
      var path = url.parse(req.url).pathname;
      function emitResponse(error, data) {
        if(error) throw error;
        res.writeHead(200);
        res.end(data);
      }
    
      switch(path) {
        case '/subpage':
        case '/subpage.html':
          fs.readFile(__dirname + '/subpage.html', function(error, data) {
            var db = new mongo.Db('mydb', new mongo.Server('localhost', '27017', {}), {});
            db.open(function() {
              db.collection('mytable', function(error, collection) {
                collection.find(function (error, cursor) {
                  data = data.replace('__INJECTED_HTML_CONTENT__', fnWhateverOneDoesWithACursor(cursor));
                  emitResponse(error, data);
                });
              });
            });
          });
          break;
        default:
          doc = fs.readFile(__dirname + '/index.html', emitResponse);
          break;
      }
    }

    But it could still be a lot nicer. If we update the code to follow a few best practices:

    • Using Express to organize the site
    • Using Step to organize async workflows
    • Using a template instead of string manipulation
    • Proper error handling w/ stack traces sent to client (while running in “development” mode)

    Best Practices Code Sample:

    server.js

    var _       = require('underscore');
    var express = require('express');
    var Step    = require('step');
    var mongo   = require('mongo');
    
    var app = express.createServer(
      // these filters run in order to process the incoming request
      // can also add cookie parsing / sessions / request logging / etc
      express.logger()
      ,express.static(__dirname + '/public')
    );
    
    var mongoDbServer = new mongo.Server('localhost', '27017', {});
    
    function fetchDbData(cb) {
      var db;
      Step(
        function stepDbConnect() {
          db = new mongo.Db('mydb', mongoDbServer, {});
          db.open(this); // this is the callback. runs the next "step"
        }
        ,function stepDbGetCollection(err, p_db) {
          if(err) throw err; // percolates errors to the next step
          db.collection('mytable', this);
        }
        ,function stepDbFind(err, collection) {
          if(err) throw err;
          collection.find(this);
        }
        ,function stepEnd(err, cursor) {
          db.end();
          cb(err, cursor);
        }
      );
    }
    
    app.get('/subpage(.html)?', function(req, res, next) {
      fetchDbData(function(err, rows) {
        if(err) next(err);
        else res.render('subpage.ejs', {rows: rows});
      });
    });
    
    app.listen(3000);
    

    views/subpage.ejs:

    <html>
    <body>
      <%= partial('row.jade', rows); %>
    </body>
    </html>
    

    views/row.jade:

    
    p Row #{row.name} blah blah blah
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a large list of static data from a server that i load
i have a uitableview which will load a uiwebview with a long page (static
I want my HTML page to load like normal, but I want one section
In my application,I want to display some static files(.html,.htm,.txt) which will be uploaded by
I am using .load() to pull static HTML files onto my main HTML page.
I want to extract a couple of links from an html page downloaded from
I have a static HTML page that allows the user to log in and
I have a page that i want to remain static with two divs that
What is the best way to load static data files containing game level maps
ViewA load some data from Coredata, then push to the next viewB . In

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.