{"id":303464,"date":"2020-05-12T21:00:30","date_gmt":"2020-05-12T21:00:30","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=303464"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=303464","title":{"rendered":"Nuxt as fullstack server: frontend + backend API Server (\u0427\u0430\u0441\u0442\u044c 1)"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/501652\/\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/id\/da\/9y\/idda9yg7oiwb8bqqn5y8w2-u4ja.jpeg\" alt=\"Nuxt as fullstack server: frontend + backend API Server\"><\/p>\n<p>  \u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 <b>Nuxt<\/b> \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e\u0442 3 \u043c\u0435\u0442\u043e\u0434\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a API:  <\/p>\n<ol>\n<li>\u0412\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 \u0432 <b>Nuxt<\/b> \u0441\u0435\u0440\u0432\u0435\u0440 <a href=\"https:\/\/github.com\/senchalabs\/connect\" rel=\"nofollow\">Connect<\/a> \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <a href=\"https:\/\/nuxtjs.org\/api\/configuration-servermiddleware\/\" rel=\"nofollow\">serverMiddleware<\/a><\/li>\n<li><a href=\"https:\/\/nuxtjs.org\/guide\/installation#using-code-create-nuxt-app-code-\" rel=\"nofollow\">\u0418\u043d\u0442\u0435\u0433\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0438<\/a> (<b>Express, Koa, Hapi<\/b> \u0438 \u0442.\u0434.)<\/li>\n<li>\u0412\u043d\u0435\u0448\u043d\u0438\u0435 API \u0441\u0435\u0440\u0432\u0435\u0440\u0430<\/li>\n<\/ol>\n<p>  \u042f \u043f\u043e\u043a\u0430\u0436\u0443 \u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043a\u0430\u043a \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u043e\u0432\u0430\u0442\u044c API \u0441\u0435\u0440\u0432\u0435\u0440 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c <b>serverMiddleware<\/b> \u043d\u0430 \u0442\u043e\u043c \u0436\u0435 \u0438\u043d\u0441\u0442\u0430\u043d\u0441\u0435 <b>Nuxt<\/b>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443 \u043d\u0430\u0441 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 frontend.<br \/>  <a name=\"habracut\"><\/a><br \/>  \u041a\u0430\u043a\u0438\u0435 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 \u0434\u0430\u0451\u0442 \u044d\u0442\u043e\u0442 \u043f\u043e\u0434\u0445\u043e\u0434:  <\/p>\n<ul>\n<li>\u0411\u044b\u0441\u0442\u0440\u044b\u0439 \u0441\u0442\u0430\u0440\u0442 \u0438 \u043c\u0438\u043d\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0442\u0435\u043a\u0430 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0439, \u043d\u0443\u0436\u0435\u043d \u0442\u043e\u043b\u044c\u043a\u043e <b>Nuxt<\/b>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 <b>fullstack server<\/b>: \u0440\u0435\u043d\u0434\u0435\u0440\u0438\u0442\u044c <b>frontend<\/b> \u0441\u0442\u0440\u0430\u043d\u0438\u0447\u043a\u0438 \u0438 \u0432\u044b\u0441\u0442\u0443\u043f\u0430\u0442\u044c \u0432 \u0440\u043e\u043b\u0438 <b>backend API \u0441\u0435\u0440\u0432\u0435\u0440\u0430<\/b><\/li>\n<li>\u041c\u0438\u043d\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c \u0442\u043e\u043b\u044c\u043a\u043e 1 \u0438\u043d\u0441\u0442\u0430\u043d\u0441 <b>Node<\/b> \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 <b>Nuxt<\/b>, \u0434\u043b\u044f \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u0438 \u0430\u0434\u043c\u0438\u043d\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u044b\u0445 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043d\u044b\u0445 \u0437\u0432\u0435\u043d\u044c\u0435\u0432 (<b>Nginx, php<\/b> \u0438 \u0442.\u0434.). \u0414\u043b\u044f \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u043e\u0433\u043e \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c <b>Nuxt<\/b> \u0438 \u043a\u0430\u043a\u0430\u044f-\u043b\u0438\u0431\u043e \u0431\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 (<b>MySQL, PostgreSQL, MongoDB<\/b> \u0438 \u0442.\u0434.)<\/li>\n<li>\u0418 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u044b\u0439, \u0438 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0438\u0439 \u043a\u043e\u0434 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0430\u043f\u0438\u0441\u0430\u043d \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u044f\u0437\u044b\u043a\u0435. \u041a\u043e\u0434\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u0438 \u0432\u0441\u044f \u043b\u043e\u0433\u0438\u043a\u0430, \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u0430\u044f \u0438 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0430\u044f \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 \u043e\u0434\u043d\u043e\u043c \u043c\u0435\u0441\u0442\u0435, \u0438 \u0432 \u043e\u0434\u043d\u043e\u043c \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u0430\u0440\u0438\u0438.<\/li>\n<li>\u0412\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 \u0432 <b>Nuxt<\/b> \u0441\u0435\u0440\u0432\u0435\u0440 <b>Connect<\/b>, \u043e\u0434\u0438\u043d \u0438\u0437 \u0441\u0430\u043c\u044b\u0445 \u0431\u044b\u0441\u0442\u0440\u044b\u0445 <b>Node<\/b> \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u0432<br \/>  <a href=\"https:\/\/github.com\/fastify\/benchmarks#benchmarks\" rel=\"nofollow\">github.com\/fastify\/benchmarks#benchmarks<\/a><br \/>  \u043c\u043e\u0436\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 55 \u0442\u044b\u0441\u044f\u0447 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443<br \/>  \u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u0435\u0441\u043b\u0438 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u043d\u044b\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b, \u0442\u043e \u0432\u0441\u0435\u0433\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043a <b>Nuxt<\/b>&#8216;\u0443 \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u043b\u043d\u043e\u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0435 \u0438\u043d\u0442\u0435\u0433\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0444\u0440\u0435\u043c\u0432\u043e\u0440\u043a\u0438 (<b>Express, Koa, Hapi<\/b> \u0438 \u0442.\u0434.) \u0438\u043b\u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u043f\u0430\u043a\u0435\u0442\u0430\u043c\u0438.<\/li>\n<li>\u041d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u0438\u0437\u043c, \u043c\u044b \u0438\u043c\u0435\u0435\u043c \u043f\u043e\u043b\u043d\u0443\u044e \u0441\u0432\u043e\u0431\u043e\u0434\u0443 \u0432 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043b\u0435\u0433\u043a\u043e \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e <b>fullstack Nuxt \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u0432<\/b>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u044e\u0442 \u0441 \u043e\u0434\u043d\u043e\u0439 \u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445, \u043f\u043b\u044e\u0441 \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u044c <b>nginx<\/b>, \u0434\u043b\u044f \u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u043a\u0438 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043c\u0435\u0436\u0434\u0443 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 <b>fullstack Nuxt \u0441\u0435\u0440\u0432\u0435\u0440\u0430\u043c\u0438<\/b>. \u0422\u0430\u043a\u0436\u0435 \u0441 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u043c\u0438 \u0434\u043e\u0440\u0430\u0431\u043e\u0442\u043a\u0430\u043c\u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u043d\u0441\u0442\u0430\u043d\u0441 <b>Nuxt<\/b>&#8216;\u0430 \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0441\u0442\u0443\u043f\u0430\u0442\u044c \u0432 \u0440\u043e\u043b\u0438 \u0442\u043e\u043b\u044c\u043a\u043e API \u0441\u0435\u0440\u0432\u0435\u0440\u0430 (only backend server), \u0442\u0430\u043a \u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u0444\u0440\u043e\u043d\u0442\u044d\u043d\u0434 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 (only frontend server).<\/li>\n<li><b>\u0418 \u043e\u0433\u0440\u043e\u043c\u043d\u044b\u0439 \u0431\u043e\u043d\u0443\u0441!!!<\/b> \u041f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u0438\u043d\u0441\u0442\u0430\u043d\u0441\u0430 \u0434\u043b\u044f <b>frontend<\/b>&#8216;\u0430 \u0438 <b>backend<\/b>&#8216;\u0430 \u043c\u043e\u0436\u043d\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u044b\u0439 \u0440\u0435\u043d\u0434\u0435\u0440\u0438\u043d\u0433 \u0441\u0442\u0440\u0430\u043d\u0438\u0446 (<b>SSR<\/b>), \u043e\u0431 \u044d\u0442\u043e\u043c \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u0432 <b>\u0427\u0430\u0441\u0442\u0438 2<\/b><\/li>\n<\/ul>\n<p>  \u0418\u0442\u0430\u043a, \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c.<\/p>\n<h2>\u0427\u0430\u0441\u0442\u044c 1: Nuxt as API Server<\/h2>\n<p>  <\/p>\n<ol>\n<li>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0430\u043f\u043a\u0443 <b>\/api\/<\/b> \u0432 \u043a\u043e\u0440\u043d\u0435 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430, \u0442\u0443\u0442 \u0431\u0443\u0434\u0443\u0442 \u043b\u0435\u0436\u0430\u0442\u044c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b \u043d\u0430\u0448\u0435\u0433\u043e API \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435<\/li>\n<li><b>API Server<\/b> \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0432\u043d\u0435\u0448\u043d\u0438\u0445 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432:<br \/>  \/serverMiddleware\/api-server.js<br \/> \n<pre><code class=\"javascript\">export default async (req, res, next) =&gt; {   let url = req._parsedUrl.pathname.replace(\/^\\\/+|\\\/+$|\\.+\/g, &quot;&quot;);   url = url.split(&quot;\/&quot;);   let method = url.pop();   let controller = url.slice(1).join(&quot;\/&quot;);   let api = require(&quot;..\/api\/&quot; + controller);   let result = await api[method](req.params);   res.end(JSON.stringify(result)); }; <\/code><\/pre>\n<p>  <\/li>\n<li>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c \u043d\u0430\u0448 <b>API Server<\/b> \u043a <b>Nuxt<\/b> \u0447\u0435\u0440\u0435\u0437 <b>serverMiddleware<\/b><br \/>  \/nuxt.config.js<br \/> \n<pre><code class=\"javascript\">  serverMiddleware: [     { path: &quot;\/api&quot;, handler: require(&quot;body-parser&quot;).json() },     {       path: &quot;\/api&quot;,       handler: (req, res, next) =&gt; {         const url = require(&quot;url&quot;);         req.query = url.parse(req.url, true).query;         req.params = { ...req.query, ...req.body };         next();       }     },     { path: &quot;\/api&quot;, handler: &quot;~\/serverMiddleware\/api-server.js&quot; }   ], <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0432\u0441\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0432\u0438\u0434\u0430<br \/>  http:\/\/&lt;server_name&gt;\/api\/controller_name\/method_name<br \/>  \u0431\u0443\u0434\u0443\u0442 \u0438\u0441\u043a\u0430\u0442\u044c \u0432 \u043f\u0430\u043f\u043a\u0435 <b>\/api\/<\/b> \u0444\u0430\u0439\u043b <b>controller_name.js<\/b>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0441 \u0438\u043c\u0435\u043d\u0435\u043c <b>method_name<\/b> \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0435\u0451 \u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 \u043f\u0435\u0440\u0432\u044b\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u043c  <\/li>\n<li>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 <b>users<\/b><br \/>  \/api\/users.js<br \/> \n<pre><code class=\"javascript\">\/\/ we can get data from any DB async function getDataFromDB() {   return (await require(&quot;axios&quot;).get(`https:\/\/reqres.in\/api\/users?page=1`))     .data; }  async function index() {   let res = await getDataFromDB();    \/\/ some business logic   let ret = res.data.map(el =&gt; {     return {       id: el.id,       full_name: el.first_name + &quot; &quot; + el.last_name,       highlight: el.first_name.charAt(0) === &quot;J&quot;     };   });   ret = ret.sort(() =&gt; 0.5 - Math.random());    return ret; }  export { index }; <\/code><\/pre>\n<p>  \u0421\u0435\u0439\u0447\u0430\u0441 \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c, \u0447\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c<br \/>  <a href=\"https:\/\/3gzhl.sse.codesandbox.io\/api\/users\/index\" rel=\"nofollow\">3gzhl.sse.codesandbox.io\/api\/users\/index<\/a>  <\/p>\n<pre><code class=\"json\">[   {     &quot;id&quot;: 3,     &quot;full_name&quot;: &quot;Emma Wong&quot;,     &quot;highlight&quot;: false   },   {     &quot;id&quot;: 5,     &quot;full_name&quot;: &quot;Charles Morris&quot;,     &quot;highlight&quot;: false   },   {     &quot;id&quot;: 1,     &quot;full_name&quot;: &quot;George Bluth&quot;,     &quot;highlight&quot;: false   },   {     &quot;id&quot;: 2,     &quot;full_name&quot;: &quot;Janet Weaver&quot;,     &quot;highlight&quot;: true   },   {     &quot;id&quot;: 4,     &quot;full_name&quot;: &quot;Eve Holt&quot;,     &quot;highlight&quot;: false   },   {     &quot;id&quot;: 6,     &quot;full_name&quot;: &quot;Tracey Ramos&quot;,     &quot;highlight&quot;: false   } ] <\/code><\/pre>\n<p>  <\/li>\n<li>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 <b>index.vue<\/b> \u0432\u044b\u0432\u0435\u0434\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b API<br \/>  \/pages\/index.vue<br \/> \n<pre><code class=\"xml\">&lt;template&gt;   &lt;div&gt;     &lt;h1 class=&quot;title&quot;&gt;NUXT as API Server&lt;\/h1&gt;     &lt;h2 class=&quot;subtitle&quot;&gt;Users&lt;\/h2&gt;     &lt;button type=&quot;button&quot; @click=&quot;$fetch&quot;&gt;Refresh&lt;\/button&gt;     &lt;ol&gt;       &lt;li         v-for=&quot;user in users&quot;         :class=&quot;{'highlight': user.highlight}&quot;         :key=&quot;user.id&quot;       &gt;{{ user.full_name }}&lt;\/li&gt;     &lt;\/ol&gt;   &lt;\/div&gt; &lt;\/template&gt;  &lt;script&gt; export default {   data() {     return {       users: []     };   },   async fetch() {     try {       this.users = await this.$axios.$get(&quot;\/api\/users\/index&quot;);     } catch (e) {       console.error(e);       throw e;     }   } }; &lt;\/script&gt;  &lt;style scoped&gt; .highlight {   color: green;   font-weight: bold; } &lt;\/style&gt; <\/code><\/pre>\n<p>  \u0432\u043e\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<br \/>  <a href=\"https:\/\/3gzhl.sse.codesandbox.io\/\" rel=\"nofollow\">3gzhl.sse.codesandbox.io<\/a><br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/wl\/hb\/np\/wlhbnpm7hou1gmg6pwqxo8b26vq.png\"><\/p>\n<p>  \u0444\u0443\u043d\u043a\u0446\u0438\u044f <b>fetch<\/b> \u043e\u0442\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u043f\u0440\u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u043e\u043c \u0440\u0435\u043d\u0434\u0435\u0440\u0435\u043d\u0433\u0435, \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u0442 \u043f\u043e <b>HTTP \u0434\u0430\u043d\u043d\u044b\u0435<\/b><br \/>  <a href=\"https:\/\/3gzhl.sse.codesandbox.io\/api\/users\/index\" rel=\"nofollow\">3gzhl.sse.codesandbox.io\/api\/users\/index<\/a><br \/>  \u0441\u043d\u043e\u0432\u0430 \u043f\u043e\u043f\u0430\u0434\u0430\u0435\u0442 \u043d\u0430 \u043d\u0430\u0448 \u0436\u0435 \u0441\u0435\u0440\u0432\u0435\u0440 \u0432 API \u0438 \u0441\u0435\u0440\u0432\u0435\u0440 \u043e\u0442\u0434\u0430\u0451\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 <b>json<\/b>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f \u0438 \u043f\u0430\u0440\u0441\u0438\u0442\u0441\u044f<\/p>\n<p>  \u0415\u0441\u043b\u0438 \u0436\u0435 \u043c\u044b \u043d\u0430\u0436\u043c\u0451\u043c \u043a\u043d\u043e\u043f\u043a\u0443 <b>Refresh<\/b>, \u0442\u043e \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u043e\u043f\u044f\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u0438\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0447\u0435\u0440\u0435\u0437 <b>http<\/b> \u0443 \u043d\u0430\u0448\u0435\u0433\u043e API \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442 \u0441\u043f\u0438\u0441\u043e\u043a.  <\/li>\n<li>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u0430:<br \/>  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 API \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043f\u043e <b>users<\/b><br \/>  \/api\/users.js<br \/> \n<pre><code class=\"javascript\">... async function show({ id }) {   let res = await getDataFromDB();    return res.data.filter(el =&gt; el.id == id)[0]; }  export { index, show }; <\/code><\/pre>\n<p>  \u041f\u0440\u0438 \u043a\u043b\u0438\u043a\u0435 \u043d\u0430 users \u043e\u0442\u043e\u0431\u0440\u0430\u0437\u0438\u043c \u044d\u0442\u0443 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e<br \/>  \/pages\/index.vue  <\/p>\n<pre><code class=\"xml\">&lt;template&gt;   &lt;div&gt;     &lt;h1 class=&quot;title&quot;&gt;NUXT as API Server&lt;\/h1&gt;     &lt;h2 class=&quot;subtitle&quot;&gt;Users&lt;\/h2&gt;     &lt;button type=&quot;button&quot; @click=&quot;$fetch&quot;&gt;Refresh&lt;\/button&gt;     &lt;ol&gt;       &lt;li         v-for=&quot;user in users&quot;         :class=&quot;{'highlight': user.highlight}&quot;         :key=&quot;user.id&quot;         @click=&quot;getUserInfo(user.id)&quot;       &gt;{{ user.full_name }}&lt;\/li&gt;     &lt;\/ol&gt;     &lt;div v-if=&quot;userInfo&quot;&gt;       &lt;hr&gt;       &lt;h3&gt;{{ userInfo.first_name }} {{ userInfo.last_name }}&lt;\/h3&gt;       &lt;div&gt;Email: {{ userInfo.email }}&lt;\/div&gt;       &lt;img :src=&quot;userInfo.avatar&quot; alt=&quot;avatar&quot;&gt;     &lt;\/div&gt;   &lt;\/div&gt; &lt;\/template&gt;  &lt;script&gt; export default {   data() {     return {       users: [],       userInfo: null     };   },   async fetch() {     try {       this.users = await this.$axios.$get(&quot;\/api\/users\/index&quot;);     } catch (e) {       console.error(e);       throw e;     }   },   methods: {     async getUserInfo(id) {       try {         this.userInfo = await this.$axios.$post(&quot;\/api\/users\/show&quot;, {           id: id         });       } catch (e) {         console.error(e);         throw e;       }     }   } }; &lt;\/script&gt;  &lt;style scoped&gt; li {   cursor: pointer; } .highlight {   color: green;   font-weight: bold; } &lt;\/style&gt; <\/code><\/pre>\n<p>  \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0438 \u043a\u043b\u0438\u043a\u0435 \u043f\u043e Users, \u0441 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u043e HTTP \u0443\u0445\u043e\u0434\u0438\u0442 \u0437\u0430\u043f\u0440\u043e\u0441<br \/>  <a href=\"https:\/\/3gzhl.sse.codesandbox.io\/api\/users\/show?id=4\" rel=\"nofollow\">3gzhl.sse.codesandbox.io\/api\/users\/show?id=4<\/a><br \/>  \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0434\u043e\u043f \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0443  <\/p>\n<pre><code class=\"json\">{&quot;id&quot;:4,&quot;email&quot;:&quot;eve.holt@reqres.in&quot;,&quot;first_name&quot;:&quot;Eve&quot;,&quot;last_name&quot;:&quot;Holt&quot;,&quot;avatar&quot;:&quot;https:\/\/s3.amazonaws.com\/uifaces\/faces\/twitter\/marcoramires\/128.jpg&quot;} <\/code><\/pre>\n<p>  \u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u043c \u0438\u0445  <\/li>\n<li>\u0422.\u043a. \u043c\u044b \u0447\u0430\u0441\u0442\u043e \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430\u0448\u0438\u043c API, \u0442\u043e \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0443\u043b\u0443\u0447\u0448\u0438\u043c \u043a\u043e\u0434<br \/>  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u043b\u0430\u0433\u0438\u043d \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0430\u0448\u0435\u0433\u043e API<br \/>  \/plugins\/api-context.js<br \/> \n<pre><code class=\"javascript\">export default (context, inject) =&gt; {     inject('api', async (controller, method, params) =&gt; {         try {             return await context.$axios['$' + (params ? 'post' : 'get')]('\/api\/' + controller + '\/' + method, params);         } catch (e) {             console.error(e);             throw e;         }     }) } <\/code><\/pre>\n<p>  \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c \u043f\u043b\u0430\u0433\u0438\u043d<br \/>  nuxt.config.js  <\/p>\n<pre><code class=\"javascript\">export default {   ...   plugins: [&quot;~\/plugins\/api-context.js&quot;] } <\/code><\/pre>\n<p>  \u0442\u0435\u043f\u0435\u0440\u044c \u0432\u0441\u0435 \u0432\u044b\u0437\u043e\u0432\u044b \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 (\u0438 \u0432 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u0445) \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0432\u044b\u0437\u043e\u0432<\/p>\n<p>  \/pages\/index.vue  <\/p>\n<pre><code class=\"javascript\">... &lt;script&gt; export default {   ...   async fetch() {     this.users = await this.$api(&quot;users&quot;, &quot;index&quot;);   },   methods: {     async getUserInfo(id) {       this.userInfo = await this.$api(&quot;users&quot;, &quot;show&quot;, {id: id});     }   } }; &lt;\/script&gt; <\/code><\/pre>\n<p>  <\/li>\n<\/ol>\n<h3>\u0418\u0442\u043e\u0433\u043e \u0427\u0430\u0441\u0442\u044c 1<\/h3>\n<p>  \u0423 \u043d\u0430\u0441 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0441\u044f <b>Nuxt<\/b> c \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u044b\u043c API \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c.  <\/p>\n<ul>\n<li>\u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043b\u0435\u0436\u0430\u0442 \u0432 \u043f\u0430\u043f\u043a\u0435 <b>\/api\/<\/b><\/li>\n<li>\u043b\u043e\u0433\u0438\u043a\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 API \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c \u043b\u0435\u0436\u0438\u0442<br \/>  <b>\/serverMiddleware\/api-server.js<\/b><\/li>\n<li>\u043b\u0435\u0433\u043a\u043e \u043c\u043e\u0436\u0435\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b <b>API \u0441\u0435\u0440\u0432\u0435\u0440\u0430<\/b>, \u0434\u043e\u0431\u0430\u0432\u0438\u0432 \u043d\u043e\u0432\u044b\u0435 middleware \u0432 <b>\/serverMiddleware\/<\/b> \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0432 \u0438\u0445 \u0432 <b>nuxt.config.js<\/b><\/li>\n<\/ul>\n<p>  \u0424\u0438\u043d\u0430\u043b\u044c\u043d\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0437\u0434\u0435\u0441\u044c:<br \/>  <a href=\"https:\/\/3gzhl.sse.codesandbox.io\/\" rel=\"nofollow\">3gzhl.sse.codesandbox.io<\/a><br \/>  \u041f\u0435\u0441\u043e\u0447\u043d\u0438\u0446\u0430 \u0437\u0434\u0435\u0441\u044c:<br \/>  <a href=\"https:\/\/codesandbox.io\/s\/codesandbox-nuxt-3gzhl\" rel=\"nofollow\">codesandbox.io\/s\/codesandbox-nuxt-3gzhl<\/a><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/501652\/\"> https:\/\/habr.com\/ru\/post\/501652\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/501652\/\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/id\/da\/9y\/idda9yg7oiwb8bqqn5y8w2-u4ja.jpeg\" alt=\"Nuxt as fullstack server: frontend + backend API Server\"><\/p>\n<p>  \u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 <b>Nuxt<\/b> \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e\u0442 3 \u043c\u0435\u0442\u043e\u0434\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a API:  <\/p>\n<ol>\n<li>\u0412\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 \u0432 <b>Nuxt<\/b> \u0441\u0435\u0440\u0432\u0435\u0440 <a href=\"https:\/\/github.com\/senchalabs\/connect\" rel=\"nofollow\">Connect<\/a> \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <a href=\"https:\/\/nuxtjs.org\/api\/configuration-servermiddleware\/\" rel=\"nofollow\">serverMiddleware<\/a><\/li>\n<li><a href=\"https:\/\/nuxtjs.org\/guide\/installation#using-code-create-nuxt-app-code-\" rel=\"nofollow\">\u0418\u043d\u0442\u0435\u0433\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0438<\/a> (<b>Express, Koa, Hapi<\/b> \u0438 \u0442.\u0434.)<\/li>\n<li>\u0412\u043d\u0435\u0448\u043d\u0438\u0435 API \u0441\u0435\u0440\u0432\u0435\u0440\u0430<\/li>\n<\/ol>\n<p>  \u042f \u043f\u043e\u043a\u0430\u0436\u0443 \u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043a\u0430\u043a \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u043e\u0432\u0430\u0442\u044c API \u0441\u0435\u0440\u0432\u0435\u0440 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c <b>serverMiddleware<\/b> \u043d\u0430 \u0442\u043e\u043c \u0436\u0435 \u0438\u043d\u0441\u0442\u0430\u043d\u0441\u0435 <b>Nuxt<\/b>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443 \u043d\u0430\u0441 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 frontend.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-303464","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/303464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=303464"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/303464\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=303464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=303464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=303464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}