(window.webpackJsonp=window.webpackJsonp||[]).push([[54],{185:function(t,s,a){"use strict";a.r(s);var n=a(0),e=Object(n.a)({},function(){var t=this,s=t.$createElement,a=t._self._c||s;return a("div",{staticClass:"content"},[t._m(0),t._v(" "),a("p",[t._v("See the "),a("router-link",{attrs:{to:"./../concepts/concepts.html#services"}},[t._v("services concept")]),t._v(" for details.")],1),t._v(" "),t._m(1),t._v(" "),a("p",[t._v("There is two ways to create a service.")]),t._v(" "),a("ul",[a("li",[t._v("Using the CLI "),a("code",[t._v("strapi generate:service user")]),t._v(". Read the "),a("router-link",{attrs:{to:"./../cli/CLI.html"}},[t._v("CLI documentation")]),t._v(" for more information.")],1),t._v(" "),t._m(2)]),t._v(" "),t._m(3),t._v(" "),t._m(4),t._v(" "),t._m(5),t._v(" "),t._m(6),t._m(7),t._v(" "),t._m(8),t._v(" "),t._m(9),t._v(" "),t._m(10)])},[function(){var t=this.$createElement,s=this._self._c||t;return s("h1",{attrs:{id:"services"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#services","aria-hidden":"true"}},[this._v("#")]),this._v(" Services")])},function(){var t=this.$createElement,s=this._self._c||t;return s("h2",{attrs:{id:"how-to-create-a-service"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#how-to-create-a-service","aria-hidden":"true"}},[this._v("#")]),this._v(" How to create a service?")])},function(){var t=this.$createElement,s=this._self._c||t;return s("li",[this._v("Manually create a JavaScript file named "),s("code",[this._v("User.js")]),this._v(" in "),s("code",[this._v("./api/**/services/")]),this._v(".")])},function(){var t=this.$createElement,s=this._self._c||t;return s("h4",{attrs:{id:"example"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#example","aria-hidden":"true"}},[this._v("#")]),this._v(" Example")])},function(){var t=this.$createElement,s=this._self._c||t;return s("p",[this._v("The goal of a service is to store reusable functions. An "),s("code",[this._v("email")]),this._v(" service could be useful, if we plan to send emails from different functions in our codebase:")])},function(){var t=this.$createElement,s=this._self._c||t;return s("p",[s("strong",[this._v("Path —")]),this._v(" "),s("code",[this._v("./api/email/services/Email.js")]),this._v(".")])},function(){var t=this,s=t.$createElement,a=t._self._c||s;return a("div",{staticClass:"language-js extra-class"},[a("pre",{pre:!0,attrs:{class:"language-js"}},[a("code",[a("span",{attrs:{class:"token keyword"}},[t._v("const")]),t._v(" nodemailer "),a("span",{attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),a("span",{attrs:{class:"token function"}},[t._v("require")]),a("span",{attrs:{class:"token punctuation"}},[t._v("(")]),a("span",{attrs:{class:"token string"}},[t._v("'nodemailer'")]),a("span",{attrs:{class:"token punctuation"}},[t._v(")")]),a("span",{attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n\n"),a("span",{attrs:{class:"token comment"}},[t._v("// Create reusable transporter object using SMTP transport.")]),t._v("\n"),a("span",{attrs:{class:"token keyword"}},[t._v("const")]),t._v(" transporter "),a("span",{attrs:{class:"token operator"}},[t._v("=")]),t._v(" nodemailer"),a("span",{attrs:{class:"token punctuation"}},[t._v(".")]),a("span",{attrs:{class:"token function"}},[t._v("createTransport")]),a("span",{attrs:{class:"token punctuation"}},[t._v("(")]),a("span",{attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n service"),a("span",{attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),a("span",{attrs:{class:"token string"}},[t._v("'Gmail'")]),a("span",{attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n auth"),a("span",{attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),a("span",{attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n user"),a("span",{attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),a("span",{attrs:{class:"token string"}},[t._v("'user@gmail.com'")]),a("span",{attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n pass"),a("span",{attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),a("span",{attrs:{class:"token string"}},[t._v("'password'")]),t._v("\n "),a("span",{attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),a("span",{attrs:{class:"token punctuation"}},[t._v("}")]),a("span",{attrs:{class:"token punctuation"}},[t._v(")")]),a("span",{attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n\nmodule"),a("span",{attrs:{class:"token punctuation"}},[t._v(".")]),t._v("exports "),a("span",{attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),a("span",{attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n send"),a("span",{attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),a("span",{attrs:{class:"token punctuation"}},[t._v("(")]),a("span",{attrs:{class:"token keyword"}},[t._v("from")]),a("span",{attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" to"),a("span",{attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" subject"),a("span",{attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" text"),a("span",{attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),a("span",{attrs:{class:"token operator"}},[t._v("=>")]),t._v(" "),a("span",{attrs:{class:"token punctuation"}},[t._v("{")]),t._v(" \n "),a("span",{attrs:{class:"token comment"}},[t._v("// Setup e-mail data.")]),t._v("\n "),a("span",{attrs:{class:"token keyword"}},[t._v("const")]),t._v(" options "),a("span",{attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),a("span",{attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),a("span",{attrs:{class:"token keyword"}},[t._v("from")]),a("span",{attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n to"),a("span",{attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n subject"),a("span",{attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n text\n "),a("span",{attrs:{class:"token punctuation"}},[t._v("}")]),a("span",{attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n\n "),a("span",{attrs:{class:"token comment"}},[t._v("// Return a promise of the function that sends the email.")]),t._v("\n "),a("span",{attrs:{class:"token keyword"}},[t._v("return")]),t._v(" transporter"),a("span",{attrs:{class:"token punctuation"}},[t._v(".")]),a("span",{attrs:{class:"token function"}},[t._v("sendMail")]),a("span",{attrs:{class:"token punctuation"}},[t._v("(")]),t._v("options"),a("span",{attrs:{class:"token punctuation"}},[t._v(")")]),a("span",{attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n "),a("span",{attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),a("span",{attrs:{class:"token punctuation"}},[t._v("}")]),a("span",{attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n")])])])},function(){var t=this.$createElement,s=this._self._c||t;return s("div",{staticClass:"note custom-block"},[s("p",[this._v("please make sure you installed "),s("code",[this._v("nodemailer")]),this._v(" ("),s("code",[this._v("npm install nodemailer")]),this._v(") for this example.")])])},function(){var t=this.$createElement,s=this._self._c||t;return s("p",[this._v("The service is now available through the "),s("code",[this._v("strapi.services")]),this._v(" global variable. We can use it in another part of our codebase. For example a controller like below:")])},function(){var t=this.$createElement,s=this._self._c||t;return s("p",[s("strong",[this._v("Path —")]),this._v(" "),s("code",[this._v("./api/user/controllers/User.js")]),this._v(".")])},function(){var t=this,s=t.$createElement,a=t._self._c||s;return a("div",{staticClass:"language-js extra-class"},[a("pre",{pre:!0,attrs:{class:"language-js"}},[a("code",[t._v("module"),a("span",{attrs:{class:"token punctuation"}},[t._v(".")]),t._v("exports "),a("span",{attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),a("span",{attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),a("span",{attrs:{class:"token comment"}},[t._v("// GET /hello")]),t._v("\n signup"),a("span",{attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),a("span",{attrs:{class:"token keyword"}},[t._v("async")]),t._v(" "),a("span",{attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctx"),a("span",{attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),a("span",{attrs:{class:"token operator"}},[t._v("=>")]),t._v(" "),a("span",{attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),a("span",{attrs:{class:"token comment"}},[t._v("// Store the new user in database.")]),t._v("\n "),a("span",{attrs:{class:"token keyword"}},[t._v("const")]),t._v(" user "),a("span",{attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),a("span",{attrs:{class:"token keyword"}},[t._v("await")]),t._v(" User"),a("span",{attrs:{class:"token punctuation"}},[t._v(".")]),a("span",{attrs:{class:"token function"}},[t._v("create")]),a("span",{attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctx"),a("span",{attrs:{class:"token punctuation"}},[t._v(".")]),t._v("params"),a("span",{attrs:{class:"token punctuation"}},[t._v(")")]),a("span",{attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n\n "),a("span",{attrs:{class:"token comment"}},[t._v("// Send an email to validate his subscriptions.")]),t._v("\n strapi"),a("span",{attrs:{class:"token punctuation"}},[t._v(".")]),t._v("services"),a("span",{attrs:{class:"token punctuation"}},[t._v(".")]),t._v("email"),a("span",{attrs:{class:"token punctuation"}},[t._v(".")]),a("span",{attrs:{class:"token function"}},[t._v("send")]),a("span",{attrs:{class:"token punctuation"}},[t._v("(")]),a("span",{attrs:{class:"token string"}},[t._v("'welcome@mysite.com'")]),a("span",{attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" user"),a("span",{attrs:{class:"token punctuation"}},[t._v(".")]),t._v("email"),a("span",{attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),a("span",{attrs:{class:"token string"}},[t._v("'Welcome'")]),a("span",{attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),a("span",{attrs:{class:"token string"}},[t._v("'...'")]),a("span",{attrs:{class:"token punctuation"}},[t._v(")")]),a("span",{attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n\n "),a("span",{attrs:{class:"token comment"}},[t._v("// Send response to the server.")]),t._v("\n ctx"),a("span",{attrs:{class:"token punctuation"}},[t._v(".")]),a("span",{attrs:{class:"token function"}},[t._v("send")]),a("span",{attrs:{class:"token punctuation"}},[t._v("(")]),a("span",{attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n ok"),a("span",{attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),a("span",{attrs:{class:"token boolean"}},[t._v("true")]),t._v("\n "),a("span",{attrs:{class:"token punctuation"}},[t._v("}")]),a("span",{attrs:{class:"token punctuation"}},[t._v(")")]),a("span",{attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n "),a("span",{attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),a("span",{attrs:{class:"token punctuation"}},[t._v("}")]),a("span",{attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n")])])])}],!1,null,null,null);e.options.__file="services.md";s.default=e.exports}}]);