{"id":281830,"date":"2016-12-01T11:45:04","date_gmt":"2016-12-01T08:45:04","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=281830"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=281830","title":{"rendered":"\u0414\u0435\u043b\u0430\u0435\u043c \u043f\u0440\u043e\u0435\u043a\u0442 \u043d\u0430 Node.js \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Mongoose, Express, Cluster. \u0427\u0430\u0441\u0442\u044c 2.1"},"content":{"rendered":"<h1 id=\"vvedenie\">\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h1>\n<p>  <\/p>\n<p>\u0417\u0434\u0440\u0430\u0441\u0442\u0432\u0443\u0439\u0442\u0435, \u0434\u043e\u0440\u043e\u0433\u0438\u0435 \u0445\u0430\u0431\u0440\u043e\u0432\u0447\u0430\u043d\u0435! \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0443 \u043d\u0430\u0441 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u0431\u0443\u0434\u0443\u0442 \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f, \u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439 \u043c\u043d\u043e\u0433\u043e.<br \/>  \u0412 \u044d\u0442\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u043c\u044b \u0431\u0443\u0434\u0435\u043c:<\/p>\n<p>  <\/p>\n<ul>\n<li>\u0421\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u0432\u043e\u0439 \u043b\u043e\u0433\u0433\u0435\u0440<\/li>\n<li>\u0417\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0432 \u043b\u043e\u0433 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0438 \u0432\u0440\u0435\u043c\u044f \u0438\u0445 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438<\/li>\n<li>\u0418\u0441\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043b\u0438 \u0432 \u043f\u0435\u0440\u0432\u043e\u0439 \u0447\u0430\u0441\u0442\u0438.<\/li>\n<li>\u0420\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0435\u0435\u0439<\/li>\n<li>\u0420\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043a\u043b\u0430\u0441\u0441\u0430\u043c\u0438<\/li>\n<li>\u041a\u043e\u043d\u0444\u0438\u0433\u0438!<\/li>\n<\/ul>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<h1 id=\"logi\">\u041b\u043e\u0433\u0438<\/h1>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u043b\u043e\u0433\u043e\u0432 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0430\u043c\u043e\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u043e\u0434\u0443\u043b\u044c. \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0430\u043f\u043a\u0443 <code>logger<\/code>. \u0412 \u043d\u0435\u043c \u0431\u0443\u0434\u0435\u0442 \u0444\u0430\u0439\u043b <code>index.js<\/code>.<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">var stackTrace = require('stack-trace');  \/\/ \u0414\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0438\u043c\u0435\u043d\u0438 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u043c\u043e\u0434\u0443\u043b\u044f var util = require('util'); \/\/util.inspect() var path = require('path'); \/\/path.relative() path.sep var projectname = require('..\/package').name; \/\/package.json -&gt; project name  module.exports = class Logger \/\/ \u041a\u043b\u0430\u0441\u0441 \u043b\u043e\u0433\u0435\u0440\u0430 :) {     constructor()     {         function generateLogFunction(level) \/\/ \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u0444\u0443\u043d\u043a\u0438\u0439 \u043b\u043e\u0433\u0433\u0435\u0440\u0430 :)         {             return function(message,meta)             {                 \/\/var d = Date.now(); \/\/ \u0411\u0443\u0434\u0435\u043c \u043f\u043e\u0442\u043e\u043c \u0437\u0430\u043f\u0438\u0441\u043e\u0432\u0430\u0442\u044c \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u0437\u043e\u0432\u0430                 var mes = this.module + &quot; -- &quot;;                 mes += level + &quot; -- &quot;;                 mes += message; \/\/ \u043f\u0440\u0438\u0446\u0435\u043f\u0438\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435                 if(meta) mes += &quot;  &quot; + util.inspect(meta) + &quot; &quot;; \/\/ \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0434\u043e\u043f \u0438\u043d\u0444\u0443 (Object||Error)                 mes += '\\n'; \/\/ \u041a\u043e\u043d\u0435\u0446 \u0441\u0442\u0440\u043e\u043a\u0438 :)                  this.write(mes);                 \/\/ \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0432\u043e \u0432\u0441\u0435 \u043f\u043e\u0442\u043e\u043a\u0438 \u043d\u0430\u0448\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435             }         };          this.trace = stackTrace.get()[1]; \/\/ \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u0442\u0435\u043a \u0432\u044b\u0437\u043e\u0432\u0430         this.filename = this.trace.getFileName(); \/\/ \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043c\u044f \u0444\u0430\u0439\u043b\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0432\u044b\u0437\u0432\u0430\u043b\u043e \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440         this.module = projectname + path.sep + path.relative('.',this.filename); \/\/ \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u043c\u0435 \u043c\u043e\u0434\u0443\u043b\u044f         this.streams = [process.stdout]; \/\/ \u041f\u043e\u0442\u043e\u043a\u0438 \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0437\u0430\u043f\u0438\u0441\u043e\u0432\u0430\u0442\u044c \u043b\u043e\u0433\u0438         \/\/ \u0412 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u0437\u0434\u0435\u0441\u044c \u0431\u0443\u0434\u0435\u0442 \u0441\u0442\u0440\u0438\u043c \u043a \u0444\u0430\u0439\u043b\u0443         this.log = generateLogFunction('Log'); \/\/ \u041b\u043e\u0433 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f         this.info = generateLogFunction('Info'); \/\/ \u041b\u043e\u0433 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0432\u043d\u044b\u0439         this.error = generateLogFunction('Error'); \/\/ \u041b\u043e\u0433 \u043e\u0448\u0438\u0431\u043e\u043a         this.warn = generateLogFunction('Warning'); \/\/ \u041b\u043e\u0433 \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0434\u0435\u043d\u0438\u0439     }     write(d)     {         this.streams.forEach((stream)=&gt;{             stream.write(d);         });     } } <\/code><\/pre>\n<p>  <\/p>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u043e \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f.<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">var logger = new require('.\/logger')(); \/\/... logger.info('Hello, world');<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u043e\u0447\u0435\u043c\u0443 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>new<\/code>? \u0414\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e \u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043c\u044f \u0444\u0430\u0439\u043b\u0430 \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0431\u044b\u043b \u0441\u043e\u0437\u0434\u0430\u043d \u043b\u043e\u0433\u0435\u0440. \u0418\u0431\u043e \u0437\u0430\u043f\u0443\u0441\u043a stack-trace \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u043f\u0438\u0448\u0435\u043c \u0432 \u043b\u043e\u0433 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432.<br \/>  \u0417\u0430\u043c\u0435\u043d\u0438\u043c \u0432\u0441\u0435 console \u043d\u0430 logger. \u041e\u0441\u0442\u0430\u0432\u043b\u044e \u0432\u0441\u0435 \u043d\u0430 \u0432\u043e\u043b\u044e \u0432\u0430\u0448\u0435\u0433\u043e IDE \ud83d\ude42<\/p>\n<p>  <\/p>\n<p>NOTE: \u0412 \u043f\u0430\u043f\u043a\u0435 <code>doc<\/code> \u0438 <code>node_modules<\/code> \u0435\u0441\u0442\u044c \u0444\u0430\u0439\u043b\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u0435 <code>console<\/code>. \u0411\u0443\u0434\u044c\u0442\u0435 \u043e\u0441\u0442\u043e\u0440\u043e\u0436\u043d\u044b!<\/p>\n<p>  <\/p>\n<p>\u0422\u0430\u043a-\u0436\u0435 \u0437\u0430\u043c\u0435\u043d\u0438\u043c \u0432 \u0444\u0430\u0439\u043b\u0435 <code>worker.js<\/code> <code>console.error<\/code> \u043d\u0430 <code>throw<\/code><br \/>  \u0412\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">app.listen(3000,function(err){     if(err) throw err;     \/\/ \u0415\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0430 \u0441\u043e\u043e\u0431\u0449\u0438\u0442\u044c \u043e\u0431 \u044d\u0442\u043e\u043c     logger.log(`Running server at port 3000!`)      \/\/ \u0418\u043d\u0430\u0447\u0435 \u0441\u043e\u043e\u0431\u0449\u0438\u0442\u044c \u0447\u0442\u043e \u043c\u044b \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u043b\u0438\u0441\u044c \u0441 \u043c\u0430\u0441\u0442\u0435\u0440\u043e\u043c     \/\/ \u0418 \u0436\u0434\u0435\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u043e\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432 });<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u043e\u0447\u0435\u043c\u0443 \u043c\u044b \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>winston<\/code> \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u043c\u043e\u0434\u0443\u043b\u0438 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043b\u043e\u0433\u0430\u043c\u0438? \u041e\u0442\u0432\u0435\u0442 \u043f\u0440\u043e\u0441\u0442: <code>winston<\/code> \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0443\u044e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c. \u0418 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u0438\u043d\u0441\u0442\u043e\u043d. \u0422\u043e\u0436\u0435 \u0441\u0430\u043c\u043e\u0435 \u043a\u0430\u0441\u0430\u0435\u0442\u0441\u044f \u043c\u043d\u043e\u0433\u0438\u0445 \u043c\u043e\u0434\u0443\u043b\u0435\u0439. \u041a\u0430\u043a \u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u043f\u043e\u0441\u043b\u0435 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0430\u0448 \u0441\u0430\u043c\u043e\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u043e\u0434\u0443\u043b\u044c \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 4-8 \u0440\u0430\u0437\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0447\u0435\u043c \u043c\u043d\u043e\u0433\u0438\u0435 \u0434\u0440\u0443\u0433\u0438\u0435 \u043c\u043e\u0434\u0443\u043b\u0438 \ud83d\ude42<\/p>\n<p>  <\/p>\n<h1 id=\"vremya-obrabotki-zaprosa\">\u0412\u0440\u0435\u043c\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/h1>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e \u0431\u044b \u0432\u0438\u0434\u0435\u0442\u044c \u043a\u0430\u043a\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043f\u0440\u0438\u0448\u043b\u0438 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440 \u0438 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0437\u0430\u043d\u044f\u043b\u0430 \u0435\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043c\u044b \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0441\u0432\u043e\u0439 middleware. \u0412 \u043f\u0430\u043f\u043a\u0435 <code>bin<\/code> \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0430\u0439\u043b <code>rt.js<\/code><\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">var Logger = require('..\/logger'); var logger = new Logger();  module.exports = function(req,res,next) {     \/\/ \u0417\u0430\u0441\u0435\u0447\u044c \u043d\u0430\u0447\u0430\u043b\u043e     var beginTime = Date.now();     \/\/ \u0412 \u043a\u043e\u043d\u0446\u0435 \u043e\u0442\u0432\u0435\u0442\u0430     res.on('finish',()=&gt;{         var d =  Date.now();\/\/ \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u0430\u0442\u0443 \u0432 \u043c\u0441         logger.log('Reponse time: ' + (d - beginTime),{             url:req.url, \/\/ \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0432 \u043b\u043e\u0433 \u043a\u0443\u0434\u0430 \u043f\u0440\u0438\u0448\u0435\u043b \u0437\u0430\u043f\u0440\u043e\u0441 (\u0412\u043a\u043b\u044e\u0447\u0430\u0435\u0442 urlencode string :)             time:(d - beginTime) \/\/ \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u043e\u0448\u043b\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438         });     });     \/\/ \u041f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u0434\u0440\u0443\u0433\u043e\u043c\u0443 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0443     next(); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0410 \u0432 <code>worker.js<\/code> \u0434\u043e \u043a\u0430\u043a\u0438\u0445 \u043b\u0438\u0431\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u0434\u043e\u0431\u0430\u0432\u0438\u043c:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">\/\/ \u0412\u0440\u0435\u043c\u044f \u043e\u0442\u0432\u0435\u0442\u0430 app.use(require('.\/rt'));<\/code><\/pre>\n<p>  <\/p>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043c\u043e\u0434\u0443\u043b\u044c \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0432\u0441\u0435 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u043c\u043e\u0434\u0443\u043b\u0438 \u041d\u0415 \u0443\u043c\u0435\u044e\u0442 \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0435 (\u0445\u043e\u0442\u044f \u0431\u044b \u0434\u043e \u041e\u0421) \u0437\u0430\u043f\u0440\u043e\u0441\u044b.<\/p>\n<p>  <\/p>\n<h1 id=\"raznica-prilozheniya-ot-routera\">\u0420\u0430\u0437\u043d\u0438\u0446\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043e\u0442 \u0440\u043e\u0443\u0442\u0435\u0440\u0430<\/h1>\n<p>  <\/p>\n<p>\u0412 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0435 \u043c\u044b \u0432\u0438\u0434\u0435\u043b\u0438 <code>express()<\/code> \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043c\u0438\u043d\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438 \u043f\u043e\u0442\u043e\u043c \u043c\u044b \u043c\u043e\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043b\u0438 \u0435\u0433\u043e \u0432 \u043f\u0440\u043e\u0435\u043a\u0442 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>app.use()<\/code> \u043d\u043e express \u043d\u0435 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e\u0442 \u0442\u0430\u043a \u0434\u0435\u043b\u0430\u0442\u044c. \u041c\u044b \u0437\u0430\u043c\u0435\u043d\u0438\u043c <code>express()<\/code> \u043d\u0430 <code>new express.Router()<\/code> \u0432 \u0444\u0430\u0439\u043b\u0435 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\"> \/\/ \u041f\u0440\u0438\u043c\u0435\u0440: var Router = require('express').Router; var app = new Router();  \/\/ app.use(....) \/\/ app.get(....) \/\/ etc<\/code><\/pre>\n<p>  <\/p>\n<p>\u041a\u0430\u043a\u0438\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442 \u0441 <code>express()<\/code>? \u0421\u0430\u043c\u043e\u0435 \u0432\u0430\u0436\u043d\u043e\u0435. \u041c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0432\u043e \u0432\u0441\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438. \u041a \u0442\u043e\u043c\u0443 \u0436\u0435 \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>app.locals<\/code>. \u0418 \u0435\u0449\u0435 \u043f\u043e \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043d\u0435 \u043f\u043e\u043d\u044f\u0442\u043d\u043e\u0439 \u043f\u0440\u0438\u0447\u0438\u043d\u0435 \u043e\u043d\u043e \u041d\u0415 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442 \u043a\u0443\u043a\u0438 (\u041f\u043e\u0447\u0435\u043c\u0443 \u0442\u0430\u043a?).<\/p>\n<p>  <\/p>\n<h1 id=\"konfiguraciya\">\u041a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f<\/h1>\n<p>  <\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0430\u043f\u043a\u0443 <code>config<\/code>.<br \/>  \u0412 \u043f\u0430\u043f\u043a\u0435 \u0443 \u043d\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 \u0444\u0430\u0439\u043b <code>index.js<\/code>, \u0433\u0434\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0432\u0441\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438, \u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0442\u044c, \u043f\u0430\u0440\u0441\u0438\u0442\u044c \u0438 \u0434\u0430\u0436\u0435 <del>\u0433\u0440\u0430\u0431\u0438\u0442\u044c \u043a\u043e\u0440\u043e\u0432\u0430\u043d\u044b<\/del> \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u043f\u0440\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043d\u0443\u0436\u043d\u044b\u0435 \u043f\u043e\u043b\u044f, \u0435\u0441\u043b\u0438 \u043e\u043d\u0438 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0442.<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">module.exports = require('.\/config');<\/code><\/pre>\n<p>  <\/p>\n<p>\u0410 \u0432 \u0444\u0430\u0439\u043b\u0435 <code>config.json<\/code>:<\/p>\n<p>  <\/p>\n<pre><code>{     &quot;port&quot;:8080,     &quot;mongoUri&quot;:&quot;mongodb:\/\/127.0.0.1\/armleo-test&quot; }<\/code><\/pre>\n<p>  <\/p>\n<p>NOTE: \u0415\u0441\u043b\u0438 \u043d\u0435\u0442 \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u043d\u044b\u0445 \u0441\u0435\u0442\u0435\u0439 \u0432 windows \u0442\u043e <code>localhost<\/code> \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0438 \u043d\u0430\u0434\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>127.0.0.1<\/code><br \/>  \u0412 \u0444\u0430\u0439\u043b\u0435 <code>worker.js<\/code> \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0432 \u043d\u0430\u0447\u0430\u043b\u0435:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">var config = require('..\/config');<\/code><\/pre>\n<p>  <\/p>\n<p>\u0410 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u044e\u0442\u0441\u044f \u0432 <del>\u0437\u0430\u0439\u0446\u0430<\/del> \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">\/\/ \u0417\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u0430 \u043f\u043e\u0440\u0442\u0443 3000 \u0438 \u0441\u043e\u043e\u0431\u0449\u0438\u043c \u043e\u0431 \u044d\u0442\u043e\u043c \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438. \/\/ \u0412\u0441\u0435 Worker-\u044b  \u0434\u043e\u043b\u0436\u043d\u044b \u0438\u043c\u0435\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u043f\u043e\u0440\u0442 app.listen(config.port,function(err){     if(err) throw err;     \/\/ \u0415\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0430 \u0441\u043e\u043e\u0431\u0449\u0438\u0442\u044c \u043e\u0431 \u044d\u0442\u043e\u043c     logger.log(`Running server at port ${config.port}!`);     \/\/ \u0418\u043d\u0430\u0447\u0435 \u0441\u043e\u043e\u0431\u0449\u0438\u0442\u044c \u0447\u0442\u043e \u043c\u044b \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u043b\u0438\u0441\u044c \u0441 \u043c\u0430\u0441\u0442\u0435\u0440\u043e\u043c     \/\/ \u0418 \u0436\u0434\u0435\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u043e\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432 });<\/code><\/pre>\n<p>  <\/p>\n<p>\u041c\u044b \u0432\u0435\u0434\u044c \u043f\u043e\u043c\u043d\u0438\u043c, \u0447\u0442\u043e \u043d\u0430\u043c \u0435\u0449\u0435 \u043d\u0443\u0436\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0438 \u0432 <code>dbinit.js<\/code>? \u0422\u0430\u043a \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u044d\u0442\u043e.<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">\/\/ 10 line bin\/dbinit.js \/\/ \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c\u0441\u044f \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443 MongoDB var config = require('..\/config'); mongoose.connect(config.mongoUri,{     server:{         poolSize: 10         \/\/ \u041f\u043e\u0441\u0442\u0430\u0432\u0438\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439 \u0432 \u043f\u0443\u043b\u0435         \/\/ 10 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u043c\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u043c\u043e\u0435\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430.         \/\/ \u0412\u0430\u043c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0438 \u0442\u043e \u043c\u0435\u043d\u044c\u0448\u0435...     } });<\/code><\/pre>\n<p>  <\/p>\n<h1 id=\"es6\">ES6<\/h1>\n<p>  <\/p>\n<p>\u041c\u044b \u0437\u0430\u043c\u0435\u043d\u0438\u043c \u0432\u0441\u0435 \u043d\u0443\u0436\u043d\u044b\u0435 <code>var<\/code> \u043d\u0430 <code>const<\/code> \u0438 <code>let<\/code><br \/>  \u041c\u0430\u043b\u0435\u043d\u044c\u043a\u043e\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435, \u043d\u043e \u043e\u043d\u043e \u043c\u043d\u0435 \u043d\u0440\u0430\u0432\u0438\u0442\u0441\u044f!<\/p>\n<p>  <\/p>\n<h1 id=\"avtorizaciya\">\u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f<\/h1>\n<p>  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043a \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438! \u0414\u043b\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <a href=\"http:\/\/passportjs.org\/\"><code>Passport.js<\/code><\/a>. \u0412 \u043d\u0430\u0448\u0435\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u041d\u0415 \u043d\u0443\u0436\u043d\u0430 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f.<br \/>  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 <code>auth.js<\/code>. \u0412 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0435 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u044b \u043f\u0430\u0440\u0441\u0435\u0440\u044b \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">let app = new (require('express').Router)();  const models = require('.\/..\/models');  const passport = require('passport'); const LocalStrategy = require('passport-local').Strategy;  app.use(passport.initialize()); app.use(passport.session());  passport.use(new LocalStrategy(     function(username, password, done) {         models.User.findOne({ username: username }, function (err, user) {         if (err) { return done(err); }         if (!user) {             return done(null, false, { message: 'Incorrect username.' });         }         if (user.password != password) {             return done(null, false, { message: 'Incorrect password.' });         }         return done(null, user);         });     } ));  passport.serializeUser(function(user, done) {     done(null, user._id); });  passport.deserializeUser(function(id, done) {     models.User.findById(id, function(err, user) {         done(err, user);     }); });  app.post('\/login',     passport.authenticate('local', {         successRedirect: '\/',         failureRedirect: '\/login'     }) ); app.get('\/login',function(req,res,next) {     if(req.user) return res.redirect('\/');      res.render('login',{         user:req.user     }); });  module.exports = app;<\/code><\/pre>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u044b \u0432 <code>views\/login.html<\/code> \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0444\u043e\u0440\u043c\u0443.<\/p>\n<p>  <\/p>\n<pre><code class=\"html\">&lt;form action=&quot;\/login&quot; method=&quot;POST&quot;&gt;     &lt;input name=&quot;username&quot;\/&gt;     &lt;br\/&gt;     &lt;input name=&quot;password&quot;\/&gt;     &lt;br\/&gt;     &lt;input type=&quot;submit&quot;\/&gt; &lt;\/form&gt;<\/code><\/pre>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c.<\/p>\n<p>  <\/p>\n<h1 id=\"posty\">\u041f\u043e\u0441\u0442\u044b!<\/h1>\n<p>  <\/p>\n<p>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c \u043c\u043e\u0434\u0443\u043b\u0438:<\/p>\n<p>  <\/p>\n<pre><code class=\"bash\">npm i mongoose-url-slugs --save<\/code><\/pre>\n<p>  <\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u043f\u043e\u0441\u0442\u043e\u0432 \u0432 \u043f\u0430\u043f\u043a\u0435 <code>models<\/code> \u0444\u0430\u0439\u043b <code>post.js<\/code>:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">\/\/ \u0417\u0430\u0433\u0440\u0443\u0437\u0438\u043c mongoose \u0442.\u043a. \u043d\u0430\u043c \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0438\u043b\u0438 \u0442\u0438\u043f\u043e\u0432 \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 const mongoose = require('mongoose'); const URLSlugs = require('mongoose-url-slugs'); \/\/ \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u043e\u0432\u0443\u044e \u0441\u0445\u0435\u043c\u0443! let postSchema = new mongoose.Schema({     title:{         type:String, \/\/ \u0442\u0438\u043f: String         required:[true,&quot;titleRequired&quot;],         \/\/ \u0414\u0430\u043d\u043d\u043e\u0435 \u043f\u043e\u043b\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e. \u0415\u0441\u043b\u0438 \u0435\u0433\u043e \u043d\u0435\u0442 \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u043e\u0448\u0438\u0431\u043a\u0443 \u0441 \u0442\u0435\u043a\u0441\u0442\u043e\u043c titleRequired         \/\/ \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0434\u043b\u0438\u043d\u043d\u0430 32 \u042e\u043d\u0438\u043a\u043e\u0434 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 (Unicode symbol != byte)         minlength:[6,&quot;tooShort&quot;],         unique:true \/\/ \u041e\u043d\u043e \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u043c     },     text:{         type:String, \/\/ \u0442\u0438\u043f String          required:[true,&quot;textRequired&quot;]         \/\/ \u0414\u0443\u043c\u0430\u044e \u0437\u0434\u0435\u0441\u044c \u0432\u0441\u0435 \u0442\u043e\u0436\u0435 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e     },     \/\/ \u0417\u0434\u0435\u0441\u044c \u0431\u0443\u0434\u0443\u0442 \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u043f\u043e\u043b\u044f, \u043d\u043e \u0441\u0435\u0439\u0447\u0430\u0441 \u0435\u0449\u0435 \u0440\u0430\u043d\u043e \u0438\u0445 \u0441\u044e\u0434\u0430 \u0441\u0442\u0430\u0432\u0438\u0442\u044c!     \/\/ \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438     \/\/ \u041e\u0446\u0435\u043d\u043a\u0438     \/\/ \u0438 \u0442\u0434      \/\/ slug:String });  \/\/ \u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c \u043f\u043b\u0430\u0433\u0438\u043d\u044b (\u0432\u043d\u0435\u0448\u043d\u0438\u0435 \u043c\u043e\u0434\u0443\u043b\u0438)  \/\/ \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f postSchema.plugin(URLSlugs('title'));  \/\/ \u041a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u043c \u0438 \u042d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c module.exports = mongoose.model('Post',postSchema);<\/code><\/pre>\n<p>  <\/p>\n<p>\u0410 \u0432 \u0444\u0430\u0439\u043b\u0435 <code>models\/index.js<\/code>:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">module.exports = {     \/\/ \u0417\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u044e\u0437\u0435\u0440\u0430 (\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f)     \/\/ \u041d\u0430 *nix-\u0430\u0445 \u0432\u0441\u0435 \u0444\u0430\u0439\u043b\u044b \u0447\u0443\u0432\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u044b \u043a \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0443     User:require('.\/user'),     Post:require('.\/post') }; \/\/ \u041d\u0435 \u0437\u0430\u0431\u0443\u0434\u0435\u043c \u0442\u043e\u0447\u043a\u0443 \u0441 \u0437\u0430\u043f\u044f\u0442\u043e\u0439!<\/code><\/pre>\n<p>  <\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f\/\u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e\u0441\u0442\u043e\u0432!<br \/>  \u0412 \u0444\u0430\u0439\u043b\u0435 <code>controllers\/index.js<\/code>:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">const Logger = require('..\/logger'); const logger = new Logger();  let app = new (require('express').Router)();  app.use(require('.\/auth')); app.use(require('.\/home')); app.use(require('.\/post'));  module.exports = app;<\/code><\/pre>\n<p>  <\/p>\n<p>\u0410 \u0432 \u0444\u0430\u0439\u043b\u0435 <code>controllers\/post.js<\/code>:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">let app = new (require('express').Router)(); const models = require(&quot;..\/models&quot;);  app.get('\/post', function(req,res,next) {     if(!req.user) return res.redirect('\/login');     res.render('addpost',{         user:req.user     }); });  app.post('\/post', function(req, res, next) {     if(!req.user) return res.redirect('\/login');     let post = new models.Post(req.body);     post.save()         .then(()=&gt;{             res.redirect('\/post\/' + post.slug);         }).catch(next); });  app.get('\/post\/:slug',(req, res, next)=&gt;{     models.Post.findOne({         slug:req.params.slug     }).exec().then((post)=&gt;{         if(!post) res.redirect('\/#notfound');         res.render('post',{             user:req.user,             post         });     }).catch(next); });  module.exports = app;<\/code><\/pre>\n<p>  <\/p>\n<p>\u0418 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0432\u0435\u043d\u043d\u043e \u043e\u0431\u0440\u0430\u0437\u044b!<br \/>  \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u044f <code>views\/addpost.html<\/code><\/p>\n<p>  <\/p>\n<pre><code class=\"html\">&lt;form method=&quot;POST&quot; action=&quot;\/post&quot;&gt;     &lt;input name=&quot;title&quot;\/&gt;     &lt;br\/&gt;     &lt;input name=&quot;text&quot;\/&gt;     &lt;br\/&gt;     &lt;input type=&quot;submit&quot;\/&gt; &lt;\/form&gt;<\/code><\/pre>\n<p>  <\/p>\n<p>\u041e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f <code>views\/post.html<\/code><\/p>\n<p>  <\/p>\n<pre><code class=\"html\">{{#post}}     &lt;h1&gt;{{title}}&lt;\/h1&gt;     &lt;br\/&gt;     {{text}} {{\/post}}<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u0434\u043e\u0434\u0435\u043b\u0430\u0435\u043c \u043e\u0431\u0440\u0430\u0437 <code>views\/index.html<\/code><\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">{{#user}}     Hello {{username}} {{\/user}} {{^user}}     Login &lt;a href=&quot;\/login&quot;&gt;here!&lt;\/a&gt; {{\/user}} {{#posts}} &lt;br\/&gt;&lt;a href=&quot;\/post\/{{slug}}&quot;&gt;{{title}}&lt;\/a&gt; {{\/posts}}<\/code><\/pre>\n<p>  <\/p>\n<p>\u0414\u043e\u0434\u0435\u043b\u0430\u0435\u043c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 <code>controllers\/home.js<\/code>:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">let app = new (require('express').Router)(); const models = require('..\/models');  app.get('\/',(req,res,next)=&gt;{     \/\/\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u044b\u0439 handler \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0438\u0434\u0438\u0442 \u043f\u043e \u043f\u0443\u0442\u0438 `\/`     models.Post.find({}).exec().then((posts)=&gt;{          res.render('index',{             user:req.user,             posts         });         \/\/ \u041e\u0442\u043f\u0440\u0430\u0432\u0438\u043c \u0440\u0435\u043d\u0434\u0435\u0440 \u043e\u0431\u0440\u0430\u0437\u0430 \u043f\u043e\u0434 \u0438\u043c\u0435\u043d\u0435\u043c index     }).catch(next); });  module.exports = app;<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412 <code>bin\/worker.js<\/code> \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u0430\u0440\u0441\u0435\u0440 urlencode:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">app.use(bodyParser.urlencoded());<\/code><\/pre>\n<p>  <\/p>\n<h1 id=\"github\">GitHub<\/h1>\n<p>  <\/p>\n<p>\u041d\u0430\u0448 \u043f\u0440\u043e\u0435\u043a\u0442 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043d\u0430\u0439\u0442\u0438 \u043d\u0430 \u0433\u0438\u0442\u0445\u0430\u0431\u0435 <a href=\"https:\/\/github.com\/armleo\/node-base\">\u0432\u043e\u0442 \u0442\u0443\u0442<\/a><\/p>\n<p>  <\/p>\n<h1 id=\"konec-vtoroy-chasti\">\u041a\u043e\u043d\u0435\u0446 \u0432\u0442\u043e\u0440\u043e\u0439 \u0447\u0430\u0441\u0442\u0438!<\/h1>\n<p>  <\/p>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u043a\u043e\u043d\u0435\u0446 \u0432\u0442\u043e\u0440\u043e\u0439 \u0447\u0430\u0441\u0442\u0438. \u0412 \u0441\u043b\u0435\u0434. \u0447\u0430\u0441\u0442\u044f\u0445 \u043c\u044b \u0432\u044b\u0434\u0435\u043b\u0438\u043c \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 HTTPS, \u0441\u0435\u0440\u0432\u0438\u0441\u0430\u043c \u0432 Ubuntu, \u0425\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0430\u0440\u043e\u043b\u044c, \u041a\u0430\u043f\u0442\u0447\u0435 (recaptcha) \u0438 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u043c \u0441 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u043e\u0439 \u0438 \u0432\u0432\u0435\u0434\u0435\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 markdown \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u043e\u0432, \u0417\u0430\u043c\u0435\u043d\u0438\u043c MongoDB \u0434\u043b\u044f \u0441\u0435\u0441\u0441\u0438\u0439 \u043d\u0430 Redis, \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.<\/p>\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:\/\/habrahabr.ru\/post\/314560\/\"> https:\/\/habrahabr.ru\/post\/314560\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<h1 id=\"vvedenie\">\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h1>\n<p>  <\/p>\n<p>\u0417\u0434\u0440\u0430\u0441\u0442\u0432\u0443\u0439\u0442\u0435, \u0434\u043e\u0440\u043e\u0433\u0438\u0435 \u0445\u0430\u0431\u0440\u043e\u0432\u0447\u0430\u043d\u0435! \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0443 \u043d\u0430\u0441 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u0431\u0443\u0434\u0443\u0442 \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f, \u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439 \u043c\u043d\u043e\u0433\u043e.<br \/>  \u0412 \u044d\u0442\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u043c\u044b \u0431\u0443\u0434\u0435\u043c:<\/p>\n<p>  <\/p>\n<ul>\n<li>\u0421\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u0432\u043e\u0439 \u043b\u043e\u0433\u0433\u0435\u0440<\/li>\n<li>\u0417\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0432 \u043b\u043e\u0433 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0438 \u0432\u0440\u0435\u043c\u044f \u0438\u0445 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438<\/li>\n<li>\u0418\u0441\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043b\u0438 \u0432 \u043f\u0435\u0440\u0432\u043e\u0439 \u0447\u0430\u0441\u0442\u0438.<\/li>\n<li>\u0420\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0435\u0435\u0439<\/li>\n<li>\u0420\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043a\u043b\u0430\u0441\u0441\u0430\u043c\u0438<\/li>\n<li>\u041a\u043e\u043d\u0444\u0438\u0433\u0438!<\/li>\n<\/ul>\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-281830","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/281830","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=281830"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/281830\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=281830"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=281830"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=281830"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}