mirror of
https://github.com/strapi/strapi.git
synced 2025-08-19 14:19:03 +00:00
1 line
7.5 KiB
JavaScript
1 line
7.5 KiB
JavaScript
![]() |
(window.webpackJsonp=window.webpackJsonp||[]).push([[21],{218:function(t,s,n){"use strict";n.r(s);var a=n(0),o=Object(a.a)({},function(){this.$createElement;this._self._c;return this._m(0)},[function(){var t=this,s=t.$createElement,n=t._self._c||s;return n("div",{staticClass:"content"},[n("h1",{attrs:{id:"services"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#services","aria-hidden":"true"}},[t._v("#")]),t._v(" Services")]),t._v(" "),n("p",[t._v("Services can be thought of as libraries which contain functions that you might want to use\nin many places of your application. For example, you might have an "),n("code",[t._v("Email")]),t._v(" service which\nwraps some default email message boilerplate code that you would want to use in many parts\nof your application.")]),t._v(" "),n("p",[t._v("Simply create a JavaScript file containing a function or an object into your\n"),n("code",[t._v("./api/<apiName>/services")]),t._v(" directory.")]),t._v(" "),n("p",[t._v("For example, you could have an "),n("code",[t._v("Email service")]),t._v(" like this:")]),t._v(" "),n("div",{staticClass:"language-js extra-class"},[n("pre",{pre:!0,attrs:{class:"language-js"}},[n("code",[n("span",{attrs:{class:"token keyword"}},[t._v("const")]),t._v(" nodemailer "),n("span",{attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{attrs:{class:"token function"}},[t._v("require")]),n("span",{attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{attrs:{class:"token string"}},[t._v("'nodemailer'")]),n("span",{attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n\nmodule"),n("span",{attrs:{class:"token punctuation"}},[t._v(".")]),t._v("exports "),n("span",{attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n sendEmail"),n("span",{attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{attrs:{class:"token keyword"}},[t._v("function")]),t._v(" "),n("span",{attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{attrs:{class:"token keyword"}},[t._v("from")]),n("span",{attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" to"),n("span",{attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" subject"),n("span",{attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" text"),n("span",{attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),n("span",{attrs:{class:"token comment"}},[t._v("// Create reusable transporter object using SMTP transport")]),t._v("\n "),n("span",{attrs:{class:"token keyword"}},[t._v("const")]),t._v(" transporter "),n("span",{attrs:{class:"token operator"}},[t._v("=")]),t._v(" nodemailer"),n("span",{attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{attrs:{class:"token function"}},[t._v("createTransport")]),n("span",{attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n service"),n("span",{attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{attrs:{class:"token string"}},[t._v("'Gmail'")]),n("span",{attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n auth"),n("span",{attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n user"),n("span",{attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{attrs:{class:"token string"}},[t._v("'gmail.user@gmail.com'")]),n("span",{attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n pass"),n("span",{attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{attrs:{class:"token string"}},[t._v("'userpass'")]),t._v("\n "),n("span",{attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n "),n("span",{attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n\n "),n("span",{attrs:{class:"token comment"}},[t._v("// Setup e-mail data")]),t._v(
|