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

  • Home
  • SEARCH
  • 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 1928510
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:59:15+00:00 2026-05-17T06:59:15+00:00

I’d like to make a node.js function that, when calls, reads a file, and

  • 0

I’d like to make a node.js function that, when calls, reads a file, and returns the contents. I’m having difficulty doing this because ‘fs’ is evented. Thus, my function has to look like this:

function render_this() {
    fs.readFile('sourcefile', 'binary', function(e, content) {
        if(e) throw e;
        // I have the content here, but how do I tell people?
    });
    return /* oh no I can't access the contents! */;
};

I know that there might be a way to do this using non-evented IO, but I’d prefer an answer that allows me to wait on evented functions so that I’m not stuck again if I come to a situation where I need to do the same thing, but not with IO. I know that this breaks the “everything is evented” idea, and I don’t plan on using it very often. However, sometimes I need a utility function that renders a haml template on the fly or something.

Finally, I know that I can call fs.readFile and cache the results early on, but that won’t work because in this situation ‘sourcefile’ may change on the fly.

  • 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-17T06:59:15+00:00Added an answer on May 17, 2026 at 6:59 am

    OK, so you want to make your development version to automatically load and re-render the file each time it changes, right?

    You can use fs.watchFile to monitor the file and then re-render the template each time it changed, I suppose you’ve got some kind of global variable in your which states whether the server is running in dev or production mode:

    var fs = require('fs');
    var http = require('http');
    var DEV_MODE = true;
    
    // Let's encapsulate all the nasty bits!
    function cachedRenderer(file, render, refresh) {
        var cachedData = null;
        function cache() {
    
            fs.readFile(file, function(e, data) {
                if (e) {
                    throw e;
                }
                cachedData = render(data);
            });
    
            // Watch the file if, needed and re-render + cache it whenever it changes
             // you may also move cachedRenderer into a different file and then use a global config option instead of the refresh parameter
            if (refresh) {
                fs.watchFile(file, {'persistent': true, 'interval': 100}, function() {
                    cache();
                });
                refresh = false;
            }
        }
    
        // simple getter
        this.getData = function() {
            return cachedData;
        }
    
        // initial cache
        cache();
    }
    
    
    var ham = new cachedRenderer('foo.haml',
    
        // supply your custom render function here
        function(data) {
            return 'RENDER' + data + 'RENDER';
        },
        DEV_MODE
    );
    
    
    // start server
    http.createServer(function(req, res) {
        res.writeHead(200);
        res.end(ham.getData());
    
    }).listen(8000);
    

    Create a cachedRenderer and then access it’s getData property whenever needed, in case you’re in development mod it will automatically re-render the file each time it changes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a
I need a function that will clean a strings' special characters. I do NOT
I have some data like this: 1 2 3 4 5 9 2 6
I know there's a lot of other questions out there that deal with this

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.