简体   繁体   English

如何在Express.JS中创建自定义Controller的动作

[英]How to create custom Controller's action in Express.JS

I'm starting with Node.JS and Express.JS, I would like to follow MVC pattern. 我从Node.JS和Express.JS开始,我想遵循MVC模式。 I found Express-Resource (https://github.com/visionmedia/express-resource) It seems good but it does not solve me problem 100% because as you can see it follow REST scheme and ALL the POST requests are sent to create method, it is a problem for me, I try to explain it with an example: 我发现Express-Resource (https://github.com/visionmedia/express-resource)看起来不错,但是并不能100%解决我的问题,因为您可以看到它遵循REST方案,并且所有POST请求都被发送来创建方法,这对我来说是个问题,我尝试举一个例子来说明:

I have a control panel where I can, show, edit, create, delete a customer. 我有一个控制面板,可以在其中显示,编辑,创建,删除客户。

when I finish to edit the costumer I send the details with a POST request, if I use that module(express-resource) "create" method will be called automatically, but it is pointless to me, I would like to call actions depending of the URLs, so: 当我完成对客户的编辑后,我将发送带有POST请求的详细信息,如果我使用该模块(express-resource),则会自动调用“ create”方法,但这对我来说毫无意义,我想根据网址,因此:

/users/create: I call it when i need to create a user account. /users/create:需要创建用户帐户时调用它。 It should accepts GET and POST 它应该接受GET和POST

GET: to see the form that allow me to create the user GET:查看允许我创建用户的表格

POST: to send the informations when I finish POST:完成后发送信息

So I always would like a path like: 所以我总是想要一条像这样的路径:

/PATH/:action/:params (like: /users/edit/1)

But I would like to avoid calling create method, when I do not create anything. 但是当我什么都不创建时,我想避免调用create方法。

Why not using bare bone express? 为什么不使用裸骨快递?

app.get("/users/:userid/create", function (req, res, next) {
    // serve the form
});

app.post("/users/:userid/create", function (req, res, next) {
    // save in the database
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM