Dail
Dail

Reputation: 4606

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. 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:

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:

/users/create: I call it when i need to create a user account. It should accepts GET and POST

GET: to see the form that allow me to create the user

POST: to send the informations when I finish

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.

Upvotes: 0

Views: 1425

Answers (1)

Jan Jongboom
Jan Jongboom

Reputation: 27313

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

Upvotes: 1

Related Questions