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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:00:47+00:00 2026-05-23T01:00:47+00:00

Sorry, still reasonably new to Javascript here, so hope this question isn’t too embarrassingly

  • 0

Sorry, still reasonably new to Javascript here, so hope this question isn’t too embarrassingly easy, but:

I’m finding I’m writing a lot of code in node.js (express) along the following lines:

app.get("urlscheme1", function (res, resp) {

  try {
     auth_request(req);  // throws on failure
     validate_url_params(req);  // throws on failure
     common_tasks();

     specific_taskABC();
  } catch (e) {
     if (e.error == "auth") {
         resp.send(....);
     } else if (e.error == "url_scheme") {
         resp.send(....);
     } else {
         resp.send(translate_error(e), code_for_error(e)):
     }
  }

});

app.put("urlscheme1", function (res, resp) {

  try {
     auth_request(req);  // throws on failure
     validate_url_params(req);  // throws on failure
     common_tasks();

     specific_taskDEF();
  } catch (e) {
     if (e.error == "auth") {
         resp.send(....);
     } else if (e.error == "url_scheme") {
         resp.send(....);
     } else {
         resp.send(translate_error(e), code_for_error(e)):
     }
  }

});


app.post("urlscheme1", function (res, resp) {

  try {
     auth_request(req);  // throws on failure
     validate_url_params(req);  // throws on failure
     common_tasks();

     specific_taskGHI();
  } catch (e) {
     if (e.error == "auth") {
         resp.send(....);
     } else if (e.error == "url_scheme") {
         resp.send(....);
     } else {
         resp.send(translate_error(e), code_for_error(e)):
     }
  }

});

This seems HORRIBLY wasteful. But, I’m not 100% comfortable with all the prototype and “subclassing” syntax/semantics in JS enough yet to understand how to make this better. Is there some way to extend an existing class (express app in this instance) to let me do something like:

app.get("urlscheme1", function(res, resp) {
  do_something_ABC();
});
app.get("urlscheme1", function(res, resp) {
  do_something_DEF();
});
app.get("urlscheme1", function(res, resp) {
  do_something_GHI();
});

where all of these handler functions are still executing that “common” code?

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-05-23T01:00:48+00:00Added an answer on May 23, 2026 at 1:00 am

    Something like this would work:

    function install(urlscheme, method, specific_task) {
    
      function handler(res, resp) {
          try {
             auth_request(req);  // throws on failure
             validate_url_params(req);  // throws on failure
             common_tasks();
             specific_task();
          } catch (e) {
             if (e.error == "auth") {
                 resp.send(....);
             } else if (e.error == "url_scheme") {
                 resp.send(....);
             } else {
                 resp.send(translate_error(e), code_for_error(e)):
             }
          }
        }
    
      app[method](urlscheme,handler); // app.post(), app.get(), etc.
    };
    
    install("urlscheme1","get", do_something_ABC );
    install("urlscheme1","post", do_something_DEF );
    install("urlscheme1","put", do_something_GHI );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry, I am still new using jquery so this might be an easy question.
I'm sorry if this is a very simplistic question but I'm still learning the
Sorry for this not being a real question, but Sometime back i remember seeing
Sorry if this sounds like a really stupid question, but I need to make
Sorry for this basic question, but I've been looking over all the info about
(Sorry if this sounds like a rant, but it's a real question and I'd
Sorry if this sounds like a bit of a noobie question, i'm still relatively
sorry but i'm still struggling to make this code working. It works if with
Guys, sorry for the basic question. I try to find out but I still
Sorry this is probably a really silly question but I am developing a WPF

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.