{"id":213931,"date":"2014-02-26T11:17:03","date_gmt":"2014-02-26T07:17:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=213931"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=213931","title":{"rendered":"<span class=\"post_title\">\u041f\u0438\u0448\u0435\u043c \u043f\u0435\u0440\u0432\u043e\u0435 REST API \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043d\u0430 NodeJS (Express + Mongoose)<\/span>"},"content":{"rendered":"<div class=\"content html_format\"> \t\t\t\u0414\u0430\u043d\u043d\u044b\u0439 \u0442\u043e\u043f\u0438\u043a \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0445 NodeJS \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432. \u041a\u043e\u0433\u0434\u0430 \u044f \u0433\u043e\u0432\u043e\u0440\u044e \u043e \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0435\u043c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0435, \u0442\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u044e \u043f\u043e\u0434 \u044d\u0442\u0438\u043c, \u0447\u0442\u043e \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u043d\u0430\u043d\u0438\u044f \u043e NodeJS \u0443 \u0432\u0430\u0441 \u0443\u0436\u0435 \u0438\u043c\u0435\u044e\u0442\u0441\u044f. \u0418\u043d\u0430\u0447\u0435 \u043d\u0430\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441 \u043e\u0441\u043d\u043e\u0432\u0430\u043c\u0438 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <a href=\"http:\/\/learn.javascript.ru\/nodejs-screencast\">\u0437\u0434\u0435\u0441\u044c<\/a>) \u0438 \u043b\u0438\u0448\u044c \u043f\u043e\u0442\u043e\u043c \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u043a \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435. <\/p>\n<p>  \u0422\u0430\u043a \u0437\u0430\u0447\u0435\u043c \u0436\u0435 \u043d\u0443\u0436\u0435\u043d \u044d\u0442\u043e\u0442 \u0442\u043e\u043f\u0438\u043a, \u0432\u0435\u0434\u044c \u0432 \u0441\u0435\u0442\u0438 \u043e\u0433\u0440\u043e\u043c\u043d\u043e\u0435 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u043e\u0432 \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c <a href=\"http:\/\/ru.wikipedia.org\/wiki\/Model-View-Controller\">MVC<\/a> \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 <a href=\"https:\/\/github.com\/visionmedia\/express\">Express<\/a>? \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u043e\u0442 \u044d\u0442\u0430 <a href=\"http:\/\/habrahabr.ru\/post\/207930\/\">\u0441\u0442\u0430\u0442\u044c\u044f<\/a> \u0438 \u043c\u043d\u043e\u0433\u0438\u0435 \u0434\u0440\u0443\u0433\u0438\u0435. \u0412\u0435\u0441\u044c \u0438\u0445 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0434\u043b\u044f \u043d\u0430\u0441 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043e\u043d\u0438 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0442 MVC \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0435 \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u043e\u0442 <a href=\"http:\/\/habrahabr.ru\/post\/38730\/\">REST<\/a> \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439. \u0418 \u0432\u043e\u0442 \u0442\u0443\u0442 \u0443 \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0435\u0433\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u0440\u044f\u0434 \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432:<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<ul>\n<li>\u0410 \u043d\u0443\u0436\u043d\u044b \u043b\u0438 \u043d\u0430\u043c \u0442\u0435\u043f\u0435\u0440\u044c view \u0444\u0430\u0439\u043b\u044b?<\/li>\n<li>\u0410 \u043d\u0443\u0436\u043d\u044b \u043b\u0438 \u043d\u0430\u043c \u0442\u0435\u043f\u0435\u0440\u044c controllers \u0438 \u0435\u0441\u043b\u0438 \u0434\u0430, \u0442\u043e \u043a\u0430\u043a \u043e\u043d\u0438 \u0431\u0443\u0434\u0443\u0442 \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u044b?<\/li>\n<li>\u041a\u0430\u043a \u0442\u0435\u043f\u0435\u0440\u044c \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0444\u0430\u0439\u043b\u043e\u0432?<\/li>\n<li>\u0438 \u0442\u0430\u043a \u0434\u0430\u043b\u0435\u0435&#8230;<\/li>\n<\/ul>\n<p>  \u0422\u0430\u043a \u043f\u043e\u0447\u0435\u043c\u0443 \u0436\u0435 \u0438\u043c\u0435\u043d\u043d\u043e Express, \u0432\u0435\u0434\u044c \u0435\u0441\u0442\u044c \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0438 (<a href=\"http:\/\/mcavage.me\/node-restify\/\">Restify<\/a>, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u044b \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u044f REST \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439? \u041f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u043e\u043d \u0438\u043c\u0435\u0435\u0442 \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0435\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u0441\u0442\u0432\u043e \u0432 \u0441\u0435\u0442\u0438, \u0438 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u0442\u044c\u0441\u044f \u0441 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043e\u0439, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0438\u043a\u0442\u043e \u0440\u0430\u043d\u044c\u0448\u0435 \u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u043b, \u2014 \u043a\u0440\u0430\u0439\u043d\u0435 \u043c\u0430\u043b\u0430. \u041d\u0430 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u044d\u0442\u043e \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0432\u0430\u0436\u043d\u043e \u0438 \u0441\u043f\u0430\u0441\u0435\u0442 \u0432\u0430\u0448\u0435 \u0434\u0440\u0430\u0433\u043e\u0446\u0435\u043d\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f. \u0412 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u043d\u0438\u043a\u0442\u043e \u043d\u0435 \u043c\u0435\u0448\u0430\u0435\u0442 \u0432\u0430\u043c \u043d\u0430\u0447\u0430\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u0431\u043e\u043b\u0435\u0435 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u0435\u0441\u043a\u043e\u0435, \u043d\u043e \u0441\u0435\u0439\u0447\u0430\u0441 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c\u0441\u044f \u0438\u043c\u0435\u043d\u043d\u043e \u043d\u0430 Express.<\/p>\n<p>  \u041d\u0443, \u0445\u0432\u0430\u0442\u0438\u0442 \u0442\u0435\u043e\u0440\u0438\u0438, \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c! \u0414\u043b\u044f \u0442\u0435\u0445, \u043a\u0442\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 Express \u0438\u043b\u0438 Mongoose, \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0435\u0439 (<a href=\"http:\/\/expressjs.com\/guide.html\">Express<\/a> \u0438 <a href=\"http:\/\/mongoosejs.com\/docs\/index.html\">Mongoose<\/a>).<\/p>\n<p>  \u0424\u0430\u0439\u043b\u043e\u0432\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0431\u0443\u0434\u0435\u0442 \u0442\u0430\u043a\u043e\u0432\u0430:<\/p>\n<pre><code>\/config     main.json \/handlers     entities.js \/libs     config.js     crudHandlers     mongoose.js \/models     entities.js \/package.json \/routes.js \/server.js <\/code><\/pre>\n<p>  <b>package.json<\/b><\/p>\n<pre><code class=\"javascript\">{   &quot;name&quot;: &quot;node-rest-express-seed&quot;,   &quot;version&quot;: &quot;0.0.1&quot;,   &quot;dependencies&quot;: {     &quot;express&quot;: &quot;~3.4.8&quot;,     &quot;nconf&quot;: &quot;~0.6.9&quot;,     &quot;winston&quot;: &quot;~0.7.2&quot;,     &quot;mongoose&quot;: &quot;~3.8.6&quot;,     &quot;underscore&quot;: &quot;~1.5.2&quot;,     &quot;async&quot;: &quot;~0.2.10&quot;   } } <\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u043c\u044b \u043e\u043f\u0438\u0441\u0430\u043b\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u043e\u0434\u0443\u043b\u0435\u0439, \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u043e\u044f\u0441\u043d\u044e:<\/p>\n<ul>\n<li><a href=\"https:\/\/github.com\/flatiron\/nconf\">Nconf<\/a> \u2014 \u043c\u043e\u0434\u0443\u043b\u044c \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0435\u0439.<\/li>\n<li><a href=\"https:\/\/github.com\/flatiron\/winston\">Winston<\/a> \u2014 \u043c\u043e\u0434\u0443\u043b\u044c \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0432\u044b\u0432\u043e\u0434\u043e\u043c \u043b\u043e\u0433\u043e\u0432.<\/li>\n<li><a href=\"http:\/\/underscorejs.org\/\">Underscore<\/a> \u2014 \u043c\u043e\u0434\u0443\u043b\u044c \u0441 \u043d\u0430\u0431\u043e\u0440\u043e\u043c \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043e\u0431\u0435\u043a\u0442\u0430\u043c\u0438, \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c\u0438, \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f\u043c\u0438.<\/li>\n<li><a href=\"https:\/\/github.com\/caolan\/async\">Async<\/a> \u2014 \u043c\u043e\u0434\u0443\u043b\u044c, \u043f\u043e\u043c\u043e\u0433\u0430\u044e\u0449\u0438\u0439 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u00abcallback hell\u00bb \u0438 \u0443\u043c\u0435\u0435\u0449\u0438\u0439 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u043c\u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u043c\u0438 \u0441\u0430\u043c\u044b\u043c\u0438 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c\u0438.<\/li>\n<\/ul>\n<p>  <b>server.js<\/b><\/p>\n<pre><code class=\"javascript\">var express = require('express'); var path = require('path'); var winston = require('winston');  var routes = require('.\/routes'); \/\/ \u0424\u0430\u0439\u043b \u0441 \u0440\u043e\u0443\u0442\u0430\u043c var config = require('.\/libs\/config'); \/\/ \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0430\u044f \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f var db = require('.\/libs\/mongoose'); \/\/ \u0424\u0430\u0439\u043b \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0431\u0430\u0437\u043e\u0439 MongoDB  var app = express(); \/\/ \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043e\u0431\u044c\u0435\u043a\u0442 express  app.use(express.json()); \/\/ &quot;\u041e\u0431\u0443\u0447\u0430\u0435\u043c&quot; \u043d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c JSON \u0438 urlencoded \u0437\u0430\u043f\u0440\u043e\u0441\u044b app.use(express.urlencoded()); app.use(express.methodOverride()); \/\/ \u041f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c PUT \u0438 DELETE \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 WEB \u0444\u043e\u0440\u043c\u0430\u043c\u0438  \/\/ \u0415\u0441\u043b\u0438 \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u0430 \u043e\u0448\u0438\u0431\u043a\u0430 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438, \u0442\u043e \u043e\u0442\u0434\u0430\u0435\u043c 400 Bad Request app.use(function (err, req, res, next) {   console.log(err.name);   if (err.name == &quot;ValidationError&quot;) {     res.send(400, err);   } else {     next(err);   } })  \/\/ \u0415\u0441\u043b\u0438 \u0436\u0435 \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u0430 \u0438\u043d\u0430\u044f \u043e\u0448\u0438\u0431\u043a\u0430 \u0442\u043e \u043e\u0442\u0434\u0430\u0435\u043c 500 Internal Server Error app.use(function (err, req, res, next) {   res.send(500, err); });  \/\/ \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c Handlers var handlers = {   entities: require('.\/handlers\/entities') }  \/\/ \u041c\u0435\u0442\u043e\u0434 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0430 function run() {   routes.setup(app, handlers); \/\/ \u0421\u0432\u044f\u0437\u0443\u0435\u043c Handlers \u0441 Routes   app.listen(config.get('port'), function () {     \/\/ \u0421\u0435\u0440\u0432\u0435\u0440 \u0437\u0430\u043f\u0443\u0449\u0435\u043d     winston.info(&quot;App running on port:&quot; + config.get('port'));   });   db.init(path.join(__dirname, &quot;models&quot;), function (err, data) {     \/\/\u0412\u044b\u0432\u043e\u0434\u0438\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043e\u0431 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0439 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445     winston.info(&quot;All the models are initialized&quot;);   }); }  if (module.parent) {   \/\/\u0415\u0441\u043b\u0438 server.js \u0437\u0430\u043f\u0443\u0449\u0435\u043d \u043a\u0430\u043a \u043c\u043e\u0434\u0443\u043b\u044c, \u0442\u043e \u043e\u0442\u0434\u0430\u0435\u043c \u043c\u043e\u0434\u0443\u043b\u044c \u0441 \u043c\u0435\u0442\u043e\u0434\u043e\u043c run   module.exports.run = run; } else {   \/\/\u0418\u043d\u0430\u0447\u0435 \u0441\u0442\u0430\u0440\u0442\u0443\u0435\u043c \u0441\u0435\u0440\u0432\u0435\u0440 \u043f\u0440\u044f\u043c\u043e \u0441\u0435\u0439\u0447\u0430\u0441   run(); } <\/code><\/pre>\n<p>  \u041a\u043e\u0434 \u043e\u0431\u0438\u043b\u044c\u043d\u043e \u0437\u0430\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0438 \u0432\u0441\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u043a\u0440\u043e\u043c\u0435 Handlers \u0438 Routes. \u041e \u043d\u0438\u0445 \u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u043e\u0437\u0436\u0435.<\/p>\n<p>  <b>libs\/config.js<\/b><\/p>\n<pre><code class=\"javascript\">\/\/ 1. \u0410\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u043a\u043e\u043c\u0430\u043d\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \/\/ 2. \u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0441\u0440\u0435\u0434\u044b \/\/ 3. \u041d\u0430\u0448 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b \u0441 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0435\u0439 var nconf = require('nconf'); nconf.argv()   .env()   .file({ file: '.\/config\/main.json' });  module.exports = nconf; <\/code><\/pre>\n<p>  \u0418 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0441\u0430\u043c \u0444\u0430\u0439\u043b \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438<\/p>\n<p>  <b>config\/main.json<\/b><\/p>\n<pre><code class=\"javascript\">{   &quot;port&quot; : 1337,   &quot;mongoose&quot;: {     &quot;uri&quot;: &quot;mongodb:\/\/localhost\/habr&quot;   } } <\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043a (\u043f\u043e\u043a\u0430). \u041c\u044b \u043b\u0438\u0448\u044c \u043e\u043f\u0438\u0448\u0435\u043c, \u043d\u0430 \u043a\u0430\u043a\u043e\u043c \u043f\u043e\u0440\u0442\u0443 \u0431\u0443\u0434\u0435\u0442 \u043a\u0440\u0443\u0442\u0438\u0442\u044c\u0441\u044f \u043d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0434\u043b\u044f MongoDB<\/p>\n<h4>Routes and Handlers<\/h4>\n<p>  \u041c\u044b \u0432\u0432\u0435\u043b\u0438 \u0434\u0432\u0430 \u043d\u043e\u0432\u044b\u0445 \u043f\u043e\u043d\u044f\u0442\u0438\u044f, \u0441\u0435\u0439\u0447\u0430\u0441 \u043f\u043e\u044f\u0441\u043d\u044e, \u0437\u0430\u0447\u0435\u043c \u043e\u043d\u0438 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u044b. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432\u0441\u043f\u043e\u043c\u043d\u0438\u0442\u044c, \u043a\u0430\u043a Express \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a \u043d\u0430\u0448\u0435\u043c\u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044e. \u0415\u0441\u043b\u0438 \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u0432 \u043e\u0431\u0449\u0435\u043c, \u0442\u043e Express \u0438\u0449\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0434\u043b\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e API \u0430\u0434\u0440\u0435\u0441\u0430, \u0438 \u0435\u0441\u043b\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u2014 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432 \u043d\u0435\u0433\u043e. \u041f\u043e \u0441\u0443\u0442\u0438 <b>Route<\/b> \u2014 \u044d\u0442\u043e:<\/p>\n<pre><code class=\"javascript\">app.get('\/v1\/blabla',someCoolFunctionForBlaBla); <\/code><\/pre>\n<p>  \u0415\u0433\u043e \u0437\u0430\u0434\u0430\u0447\u0430 \u043f\u0440\u043e\u0431\u0440\u043e\u0441\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0432\u043e \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u2014 <b>Handler<\/b>. \u041f\u0440\u0438\u043c\u0435\u0440 \u0442\u0430\u043a\u043e\u0433\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430:<\/p>\n<pre><code class=\"javascript\">var someCoolFunctionForBlaBla = function(req,res,next) {   \/\/ \u0422\u0443\u0442 \u043c\u044b \u0431\u0435\u0440\u0435\u043c \u0438\u0437 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 req \u0432\u0441\u0435 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0435, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, req.query \u0438\u043b\u0438 req.body   \/\/ \u041f\u0440\u043e\u0432\u043e\u0434\u0438\u043c \u0432\u0441\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0438 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f   res.send(&quot;Hello&quot;) \/\/ \u041e\u0442\u0434\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u0442\u0440\u043e\u043a\u0443 &quot;Hello&quot; }; <\/code><\/pre>\n<p>  Routes \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u043e\u0434\u043d\u043e\u043c \u0444\u0430\u0439\u043b\u0435, \u0430 \u0432\u043e\u0442 Handlers \u0431\u0443\u0434\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u0434\u0440\u0443\u0433\u0438\u0445. \u042d\u0442\u043e \u0434\u0430\u0441\u0442 \u043d\u0430\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043b\u043e\u0433\u0438\u043a\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u043e\u0442 \u0438\u0445 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432.<\/p>\n<p>  <b>routes.js<\/b><\/p>\n<pre><code class=\"javascript\">module.exports.setup = function (app, handlers) {   app.get('\/v1\/entities', handlers.entities.list);   app.get('\/v1\/entities\/:id', handlers.entities.get);   app.post('\/v1\/entities', handlers.entities.create);   app.put('\/v1\/entities\/:id', handlers.entities.update);   app.delete('\/v1\/entities\/:id', handlers.entities.remove); }; <\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u0432\u0441\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0435\u0441\u043b\u0438 \u043d\u0435\u0442 \u2014 \u043d\u0443\u0436\u043d\u043e \u0435\u0449\u0435 \u0440\u0430\u0437 \u043f\u0435\u0440\u0435\u0447\u0438\u0442\u0430\u0442\u044c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e Express.<\/p>\n<p>  <b>handlers\/entities.js<\/b><\/p>\n<pre><code class=\"javascript\">var mongoose = require('..\/libs\/mongoose');  \/\/ \u0412\u044b\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c modelName var modelName = 'entities'; \/\/ \u041f\u043e\u0434\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f CRUD \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432 var handlers = require('..\/libs\/crudHandlers')(modelName);  module.exports = handlers; <\/code><\/pre>\n<p>  \u042f \u0434\u0443\u043c\u0430\u044e, \u0437\u0434\u0435\u0441\u044c \u0432\u044b \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u043b\u0438 \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435 \u043a\u043e\u0434\u0430, \u0447\u0435\u043c \u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c. \u041f\u043e\u044f\u0441\u043d\u044e. \u0422\u0430\u043a \u043a\u0430\u043a <a href=\"http:\/\/ru.wikipedia.org\/wiki\/CRUD\">CRUD<\/a> \u0434\u043b\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0447\u0430\u0441\u0442\u043e \u0438\u043c\u0435\u0435\u0442 \u043e\u0434\u043d\u0443 \u0438 \u0442\u0443 \u0436\u0435 \u043b\u043e\u0433\u0438\u043a\u0443, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u043d\u0435\u0441\u0442\u0438 \u044d\u0442\u043e \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u043e\u0434\u0443\u043b\u044c. \u0415\u0441\u043b\u0438 \u0436\u0435 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u0430 \u0438\u043d\u0430\u044f \u043b\u043e\u0433\u0438\u043a\u0430, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b \u0438\u0437 crudHandlers \u0432 entities.js \u0438\u043b\u0438 \u0432\u043e\u0432\u0441\u0435 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e.<\/p>\n<p>  <b>libs\/crudHandlers.js<\/b><\/p>\n<pre><code class=\"javascript\">var mongoose = require('mongoose'); var db = require('.\/mongoose');  module.exports = function (modelName) {    \/\/ \u0421\u043f\u0438\u0441\u043e\u043a \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432   var list = function (req, res, next) {     db.model(modelName).find({}, function (err, data) {       if (err) next(err);       res.send(data);     });   };    \/\/ \u041e\u0434\u0438\u043d \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442   var get = function (req, res, next) {     try{var id = mongoose.Types.ObjectId(req.params.id)}     catch (e){res.send(400)}      db.model(modelName).find({_id: id}, function (err, data) {       if (err) next(err);       if (data) {         res.send(data);       } else {         res.send(404);       }     })   };    \/\/ \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442   var create = function (req, res, next) {      db.model(modelName).create(req.body, function (err, data) {       if (err) {         next(err);       }       res.send(data);     });   };    \/\/ \u041e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442   var update = function (req, res, next) {     try{var id = mongoose.Types.ObjectId(req.params.id)}     catch (e){res.send(400)}      db.model(modelName).update({_id: id}, {$set: req.body}, function (err, numberAffected, data) {       if (err) next(err);        if (numberAffected) {         res.send(200);       } else {         res.send(404);       }      })   };    \/\/ \u0423\u0434\u0430\u043b\u044f\u0435\u043c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442   var remove = function (req, res, next) {     try{var id = mongoose.Types.ObjectId(req.params.id)}     catch (e){res.send(400)}      db.model(modelName).remove({_id: id}, function (err, data) {       if (err) next(err);       res.send(data ? req.params.id : 404);     });   };    return {     list  : list,     get   : get,     create: create,     update: update,     remove: remove   } }; <\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u043c\u044b \u043b\u0438\u0448\u044c \u043e\u043f\u0438\u0441\u0430\u043b\u0438 CRUD \u043c\u0435\u0442\u043e\u0434\u044b, \u043e\u043d\u0438 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0443 \u0432\u0430\u0441, \u043a\u0430\u043a\u0438\u0435 \u0432\u0430\u043c \u0437\u0430\u0431\u043b\u0430\u0433\u043e\u0440\u0430\u0441\u0441\u0443\u0434\u044f\u0442\u0441\u044f. \u041d\u043e \u0447\u0442\u043e \u0436\u0435 \u0442\u0430\u043a\u043e\u0435 db.model? \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0439:<\/p>\n<p>  <b>libs\/mongoose.js<\/b><\/p>\n<pre><code class=\"javascript\">var mongoose = require('mongoose'); var fs = require('fs'); var path = require('path'); var async = require('async'); var config = require('.\/config');  mongoose.connect(config.get('mongoose:uri')); var db = mongoose.connection;  db.on('error', function (err) {   \/\/ \u041e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043e\u0448\u0438\u0431\u043a\u0443 }); db.once('open', function callback() {   \/\/ \u0421\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0448\u043b\u043e \u0443\u0441\u043f\u0435\u0448\u043d\u043e });  var models = {};  \/\/\u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u0432\u0441\u0435 \u0441\u0445\u0435\u043c\u044b var init = function (modelsDirectory, callback) {   \/\/\u0421\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u0444\u0430\u0439\u043b\u043e\u0432 \u0438\u0437 modelsDirectory   var schemaList = fs.readdirSync(modelsDirectory);   \/\/\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u0438 Mongoose \u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c callback, \u043a\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043c   async.eachSeries(schemaList, function (item, cb) {     var modelName = path.basename(item, '.js');     models[modelName] = require(path.join(modelsDirectory, modelName))(mongoose);     cb();   }, callback); };  \/\/\u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0443\u0436\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 var model = function (modelName) {   var name = modelName.toLowerCase();   if (typeof models[name] == &quot;undefined&quot;) {     \/\/ \u0415\u0441\u043b\u0438 \u043c\u043e\u0434\u0435\u043b\u044c \u043d\u0430 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043e\u0448\u0438\u0431\u043a\u0443     throw &quot;Model '&quot; + name + &quot;' is not exist&quot;;   }   return models[name]; };  module.exports.init = init; module.exports.model = model; <\/code><\/pre>\n<p>  \u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0432 \u0411\u0414 \u0443 \u043d\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c, \u0438 \u0447\u0442\u043e\u0431\u044b \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u043e\u043d\u0430 \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f, \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c require, \u043d\u0430\u043c \u0438 \u043d\u0443\u0436\u0435\u043d \u044d\u0442\u043e\u0442 \u0444\u0430\u0439\u043b.<br \/>  \u0422\u0435\u043f\u0435\u0440\u044c \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c entities:<\/p>\n<p>   <b>models\/entities.js<\/b><\/p>\n<pre><code class=\"javascript\">var path = require('path');  module.exports = function (mongoose) {    \/\/\u041e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u043c \u0441\u0445\u0435\u043c\u0443 \u0434\u043b\u044f Mongoose   var Schema = new mongoose.Schema({     name: { type: String, required: true }   });    \/\/ \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u0441 \u0438\u043c\u0435\u043d\u0435\u043c \u0444\u0430\u0439\u043b\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u043d\u0430 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f   return mongoose.model(path.basename(module.filename, '.js'), Schema); }; <\/code><\/pre>\n<p>  \u041d\u0443, \u0432\u043e\u0442 \u0438 \u0432\u0441\u0435, \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c.<br \/>  \u0418\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438 \u0432\u0441\u0435\u0433\u043e \u044d\u0442\u043e\u0433\u043e \u2014 <a href=\"https:\/\/github.com\/asynxis\/node-rest-seed\">\u0432\u043e\u0442<\/a>.<\/p>\n<p>  \u0423\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441 <b>github<\/b>:<\/p>\n<pre><code>git clone https:\/\/github.com\/asynxis\/node-rest-seed.git my-firts-app cd my-firts-app npm i node server.js <\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0432\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0442\u0430\u043a\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438:<\/p>\n<pre><code>info: All the models are initialized info: App running on port:1337 <\/code><\/pre>\n<p>  \u0415\u0441\u043b\u0438 \u044d\u0442\u043e\u0433\u043e \u043d\u0435 \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u043e, \u043f\u0440\u043e\u0432\u0435\u0440\u044c\u0442\u0435, \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u0430 \u043b\u0438 \u0443 \u0432\u0430\u0441 MongoDB \u0438 \u0441\u0432\u043e\u0431\u043e\u0434\u0435\u043d \u043b\u0438 \u0443 \u0432\u0430\u0441 \u043f\u043e\u0440\u0442 1337.<\/p>\n<h4> \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <\/h4>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0432\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0435 REST \u0441\u0435\u0440\u0432\u0435\u0440 \u0441 \u0442\u0430\u043a\u0438\u043c\u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044f\u043c\u0438:<\/p>\n<ul>\n<li>GET \/v1\/entities \u2014 \u0441\u043f\u0438\u0441\u043e\u043a<\/li>\n<li>GET \/v1\/entities\/:id \u2014 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0437\u0430\u043f\u0438\u0441\u044c \u0441 \u043a\u043b\u044e\u0447\u043e\u043c id<\/li>\n<li>POST \/v1\/entities \u2014 \u0441\u043e\u0437\u0434\u0430\u0435\u043c (\u043d\u0443\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c name \u0432 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u0445)<\/li>\n<li>PUT \/v1\/entities\/:id \u2014 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c \u043f\u043e id (\u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c\u044b\u0435 \u043f\u043e\u043b\u044f \u0442\u0430\u043a\u0436\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0432 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u0445)<\/li>\n<li>DELETE \/v1\/entities\/:id- \u0443\u0434\u0430\u043b\u044f\u0435\u043c \u043f\u043e id<\/li>\n<\/ul>\n<p>  \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0438 \u043d\u043e\u0432\u0443\u044e \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u0432 \u0411\u0414 \u0438 \u0445\u043e\u0442\u0438\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0442\u044c CRUD \u043a \u043d\u0435\u0439. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0434\u0435\u043b\u0430\u0442\u044c 4 \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u0432\u0435\u0449\u0438:<\/p>\n<ol>\n<li>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0444\u0430\u0439\u043b \u0441 \u0438\u043c\u0435\u043d\u0435\u043c \u0432\u0430\u0448\u0435\u0439 \u043d\u043e\u0432\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0432 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438 handlers \u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0442\u0430\u043c modelName.<\/li>\n<li>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0444\u0430\u0439\u043b c \u0438\u043c\u0435\u043d\u0435\u043c modelName \u0432 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 models \u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0442\u0430\u043c Mongoose Schema \u0434\u043b\u044f \u043d\u043e\u0432\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438.<\/li>\n<li>\u041e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442 handlers \u0432 server.js.<\/li>\n<li>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043d\u0443\u0436\u043d\u044b\u0435 \u043d\u0430\u043c \u0440\u043e\u0443\u0442\u044b \u0432 routes.js<\/li>\n<\/ol>\n<p>  \u0418, \u0432\u0443\u0430\u043b\u044f, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c CRUD \u043a \u0435\u0449\u0435 \u043e\u0434\u043d\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438!<br \/>  \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u0434\u0435\u043b\u0430\u0442\u044c \u0438\u0437 \u044d\u0442\u043e\u0433\u043e \u043a\u0430\u0440\u043a\u0430\u0441\u0430 \u0432\u0430\u0448\u0435 \u0441\u0443\u043f\u0435\u0440 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435!<\/p>\n<p>  P.S. <i>\u0411\u0443\u0434\u0443 \u0440\u0430\u0434 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u043c \u0438 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u043c, \u0441\u0430\u043c \u0442\u043e\u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e NodeJS \u043d\u0435\u0434\u0430\u0432\u043d\u043e. \u0425\u043e\u0447\u0435\u0442\u0441\u044f \u043f\u043e\u043c\u043e\u0447\u044c \u0442\u0435\u043c, \u043a\u0442\u043e \u0442\u0430\u043a\u0436\u0435, \u043a\u0430\u043a \u0438 \u044f, \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442!<\/i> \t\t\t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/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=\"http:\/\/habrahabr.ru\/post\/213931\/\"> http:\/\/habrahabr.ru\/post\/213931\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\"> \t\t\t\u0414\u0430\u043d\u043d\u044b\u0439 \u0442\u043e\u043f\u0438\u043a \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0445 NodeJS \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432. \u041a\u043e\u0433\u0434\u0430 \u044f \u0433\u043e\u0432\u043e\u0440\u044e \u043e \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0435\u043c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0435, \u0442\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u044e \u043f\u043e\u0434 \u044d\u0442\u0438\u043c, \u0447\u0442\u043e \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u043d\u0430\u043d\u0438\u044f \u043e NodeJS \u0443 \u0432\u0430\u0441 \u0443\u0436\u0435 \u0438\u043c\u0435\u044e\u0442\u0441\u044f. \u0418\u043d\u0430\u0447\u0435 \u043d\u0430\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441 \u043e\u0441\u043d\u043e\u0432\u0430\u043c\u0438 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <a href=\"http:\/\/learn.javascript.ru\/nodejs-screencast\">\u0437\u0434\u0435\u0441\u044c<\/a>) \u0438 \u043b\u0438\u0448\u044c \u043f\u043e\u0442\u043e\u043c \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u043a \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435. <\/p>\n<p>  \u0422\u0430\u043a \u0437\u0430\u0447\u0435\u043c \u0436\u0435 \u043d\u0443\u0436\u0435\u043d \u044d\u0442\u043e\u0442 \u0442\u043e\u043f\u0438\u043a, \u0432\u0435\u0434\u044c \u0432 \u0441\u0435\u0442\u0438 \u043e\u0433\u0440\u043e\u043c\u043d\u043e\u0435 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u043e\u0432 \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c <a href=\"http:\/\/ru.wikipedia.org\/wiki\/Model-View-Controller\">MVC<\/a> \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 <a href=\"https:\/\/github.com\/visionmedia\/express\">Express<\/a>? \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u043e\u0442 \u044d\u0442\u0430 <a href=\"http:\/\/habrahabr.ru\/post\/207930\/\">\u0441\u0442\u0430\u0442\u044c\u044f<\/a> \u0438 \u043c\u043d\u043e\u0433\u0438\u0435 \u0434\u0440\u0443\u0433\u0438\u0435. \u0412\u0435\u0441\u044c \u0438\u0445 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0434\u043b\u044f \u043d\u0430\u0441 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043e\u043d\u0438 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0442 MVC \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0435 \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u043e\u0442 <a href=\"http:\/\/habrahabr.ru\/post\/38730\/\">REST<\/a> \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439. \u0418 \u0432\u043e\u0442 \u0442\u0443\u0442 \u0443 \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0435\u0433\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u0440\u044f\u0434 \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432:  <\/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-213931","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/213931","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=213931"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/213931\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=213931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=213931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=213931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}