Skip to content

Instantly share code, notes, and snippets.

@rzvdaniel
Last active July 6, 2019 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rzvdaniel/d2c5b1c2f420f1005b64267991f7db1b to your computer and use it in GitHub Desktop.
Save rzvdaniel/d2c5b1c2f420f1005b64267991f7db1b to your computer and use it in GitHub Desktop.
[Medium] Moleculer Routing - Auto Aliases
// users.service.js
module.exports = {
name: "users",
actions: {
list: {
// Expose as "/users/"
rest: "GET /",
handler(ctx) {
return "GET Users list";
}
},
get: {
// Expose as "/users/:id"
rest: "GET /:id",
handler(ctx) {
return `GET user with Id = ${ctx.params.id}`;
}
},
create: {
// Expose as "/users/"
rest: "POST /",
params: {
name: { type: "string" }
},
handler(ctx) {
return `CREATE user with name = ${ctx.params.name}`;
}
},
update: {
// Expose as "/users/:id"
rest: "PUT /:id",
params: {
name: { type: "string" }
},
handler(ctx) {
return `UPDATE name of user with id = ${ctx.params.id}. New name: ${
ctx.params.name
}`;
}
},
remove: {
// Expose as "/users/:id"
rest: "DELETE /:id",
handler(ctx) {
return `DELETE user with id = ${ctx.params.id}`;
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment