javascript 子类node.js 表达方法来添加常用功能?
抱歉,这里对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样的事情会起作用:
Something like this would work: