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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:23:31+00:00 2026-06-05T06:23:31+00:00

I’m doing a project in Node.js using express. Here’s my directory structure: root |-start.js

  • 0

I’m doing a project in Node.js using express. Here’s my directory structure:

root
|-start.js
|-server.js
|-lib/
|    api/
|        user_getDetails.js
|        user_register.js

The lib/api/ directory has a number of JS files relating to the API. What I need to do is make a sort of hooking system, that whenever one of the API functions gets requested from the express HTTP server, it does whatever action is specified in the according API handler. It’s probably confusing, but hopefully you get the idea.

  1. Larry sends request via POST to get user details.
  2. Server looks in lib/api to find the function associated with that request.
  3. Server carrys out action and sends back data to Larry.

Hopefully you can help me out. I was thinking it could be done using prototypes, not sure though.

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-05T06:23:32+00:00Added an answer on June 5, 2026 at 6:23 am

    If you know where your scripts are, i.e. you have an initial directory, for example DIR, then you can work with fs, for example:

    server.js

    var fs = require('fs');
    var path_module = require('path');
    var module_holder = {};
    
    function LoadModules(path) {
        fs.lstat(path, function(err, stat) {
            if (stat.isDirectory()) {
                // we have a directory: do a tree walk
                fs.readdir(path, function(err, files) {
                    var f, l = files.length;
                    for (var i = 0; i < l; i++) {
                        f = path_module.join(path, files[i]);
                        LoadModules(f);
                    }
                });
            } else {
                // we have a file: load it
                require(path)(module_holder);
            }
        });
    }
    var DIR = path_module.join(__dirname, 'lib', 'api');
    LoadModules(DIR);
    
    exports.module_holder = module_holder;
    // the usual server stuff goes here
    

    Now your scripts need to follow the following structure (because of the require(path)(module_holder) line), for example:

    user_getDetails.js

    function handler(req, res) {
        console.log('Entered my cool script!');
    }
    
    module.exports = function(module_holder) {
        // the key in this dictionary can be whatever you want
        // just make sure it won't override other modules
        module_holder['user_getDetails'] = handler;
    };
    

    and now, when handling a request, you do:

    // request is supposed to fire user_getDetails script
    module_holder['user_getDetails'](req, res);
    

    This should load all your modules to module_holder variable. I didn’t test it, but it should work (except for the error handling!!!). You may want to alter this function (for example make module_holder a tree, not a one level dictionary) but I think you’ll grasp the idea.

    This function should load once per server start (if you need to fire it more often, then you are probably dealing with dynamic server-side scripting and this is a baaaaaad idea, imho). The only thing you need now is to export module_holder object so that every view handler can use it.

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

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Specifically, suppose I start with the string string =hello \'i am \' me And
I am reading a book about Javascript and jQuery and using one of the
I am doing a simple coin flipping experiment for class that involves flipping a
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.