javascript 子类node.js 表达方法来添加常用功能?

发布于 2024-11-14 02:47:10 字数 1798 浏览 1 评论 0原文

抱歉,这里对 Javascript 还比较陌生,所以希望这个问题不会太容易,但是:

我发现我在node.js(express)中编写了很多代码,如下所示:

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)):
     }
  }

});

这看起来非常浪费。但是,我对 JS 中的所有原型和“子类化”语法/语义还不是 100% 满意,还不知道如何改进。有没有某种方法可以扩展现有的类(在本例中为express app),让我做类似的事情:

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

所有这些处理程序函数仍在执行“通用”代码?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那伤。 2024-11-21 02:47:10

像这样的事情会起作用:

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 );

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 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文