{"id":617,"date":"2012-06-10T23:55:41","date_gmt":"2012-06-11T06:55:41","guid":{"rendered":"http:\/\/h2plus.biz\/hiromitsu\/?p=617"},"modified":"2018-08-06T14:03:44","modified_gmt":"2018-08-06T21:03:44","slug":"node-js%e3%81%a7rest-api%e3%82%92%e4%bd%9c%e3%82%8b%e3%81%ae%e3%81%ab%e6%9c%80%e9%81%a9%e3%81%aaexpress-resource","status":"publish","type":"post","link":"https:\/\/h2plus.biz\/hiromitsu\/entry\/617","title":{"rendered":"node.js\u3067REST API\u3092\u4f5c\u308b\u306e\u306b\u6700\u9069\u306aexpress-resource"},"content":{"rendered":"<p>\u300cnode.js\u3092\u4f7f\u3063\u3066\u7c21\u5358\u306bWeb\u30a2\u30d7\u30ea\u304c\u4f5c\u308c\u308b\uff01\u300d\u307f\u305f\u3044\u306a\u8a18\u4e8b\u3067\u7d39\u4ecb\u3055\u308c\u3066\u3044\u308b\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u306f\u3001\u3060\u3044\u305f\u3044\u305d\u306e\u307e\u307e\u4f7f\u3044\u3082\u306e\u306b\u306a\u3089\u306a\u3044\u3053\u3068\u304c\u591a\u3044\u3002\u5b9f\u969b\u306e\u30a2\u30d7\u30ea\u958b\u767a\u3067\u306f\u3001\u3044\u308d\u3044\u308d\u306a\u30e2\u30b8\u30e5\u30fc\u30eb\u3092NPM\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u3001\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u30c4\u30ea\u30fc\u306e\u968e\u5c64\u306a\u3093\u304b\u306b\u3082\u6c17\u3092\u914d\u3063\u305f\u308a\u3059\u308b\u3082\u306e\u3060\u3002DB\u3078\u306e\u30a2\u30af\u30bb\u30b9\u3082\u4e00\u5143\u5316\u3057\u3066\u3001O\/R\u30de\u30c3\u30d1\u30fc\u3082\u4f7f\u3048\u308b\u3088\u3046\u306b\u3057\u305f\u3044\u3002\u56fd\u969b\u5316\u5bfe\u5fdc\u3082\u8003\u3048\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002\u30c7\u30d0\u30c3\u30ac\u3082\u5fc5\u8981\u3060\u3002\u30a2\u30d7\u30ea\u3092\u4f5c\u308a\u306a\u304c\u3089\u5f8c\u4ed8\u3057\u3066\u3044\u304f\u306e\u3082\u3044\u3044\u304c\u3001\u6700\u521d\u306e\u6bb5\u968e\u3067\u3042\u308b\u7a0b\u5ea6\u306f\u56fa\u3081\u3066\u304a\u304d\u305f\u3044\u3068\u601d\u3046\u4eba\u3082\u591a\u3044\u3060\u308d\u3046\u3002<\/p>\n<p>\u3059\u3079\u3066\u3092\u4e00\u6c17\u306b\u7d39\u4ecb\u3059\u308b\u3068\u76f8\u5f53\u9577\u304f\u306a\u308a\u305d\u3046\u306a\u306e\u3067\u3001\u4eca\u56de\u306fREST API\u3092\u4f5c\u308b\u4e0a\u3067\u6b20\u304b\u305b\u306a\u3044express\u3068express-resource\u306b\u7126\u70b9\u3092\u5f53\u3066\u308b\u3002node.js\u3068NPM\u3092\u307e\u3060\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u3044\u306a\u304b\u3063\u305f\u3089<a href=\"http:\/\/h2plus.biz\/hiromitsu\/entry\/154\">nvm\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/a>\u3057\u3066\u304a\u3053\u3046\u3002<\/p>\n<p><!--more--><\/p>\n<h3>express<\/h3>\n<p>node.js\u3067MVC\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\u306e\u30a2\u30d7\u30ea\u3092\u4f5c\u308d\u3046\u3068\u601d\u3063\u305f\u3089\u3001\u307e\u305a\u306f<a href=\"http:\/\/expressjs.com\/\">express<\/a>\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u3001express\u3067\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u6700\u521d\u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u69cb\u6210\u3057\u3088\u3046\u3002<\/p>\n<pre>\n$ sudo npm install -g express\n$ express myrest\n\n   <span style=\"color:blue\">create<\/span> : myrest\n   <span style=\"color:blue\">create<\/span> : myrest\/package.json\n   <span style=\"color:blue\">create<\/span> : myrest\/app.js\n   <span style=\"color:blue\">create<\/span> : myrest\/public\n   <span style=\"color:blue\">create<\/span> : myrest\/public\/javascripts\n   <span style=\"color:blue\">create<\/span> : myrest\/public\/images\n   <span style=\"color:blue\">create<\/span> : myrest\/public\/stylesheets\n   <span style=\"color:blue\">create<\/span> : myrest\/public\/stylesheets\/style.css\n   <span style=\"color:blue\">create<\/span> : myrest\/routes\n   <span style=\"color:blue\">create<\/span> : myrest\/routes\/index.js\n   <span style=\"color:blue\">create<\/span> : myrest\/views\n   <span style=\"color:blue\">create<\/span> : myrest\/views\/layout.jade\n   <span style=\"color:blue\">create<\/span> : myrest\/views\/index.jade\n\n   dont forget to install dependencies:\n   $ cd myrest && npm install\n<\/pre>\n<p>root\u6a29\u9650\u3067\u30b0\u30ed\u30fc\u30d0\u30eb\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u3044\u308b\u304c\u3001\u30ed\u30fc\u30ab\u30eb\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3067\u3082\u69cb\u308f\u306a\u3044\u3002<\/p>\n<p>\u6700\u521d\u306e\u6642\u70b9\u3067\u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u69cb\u6210\u306f\u3053\u3093\u306a\u611f\u3058\u3002<\/p>\n<pre>\nmyrest\/\n\u251c\u2500\u2500 app.js\n\u251c\u2500\u2500 package.json\n\u251c\u2500\u2500 public\/\n\u2502\u00a0\u00a0 \u251c\u2500\u2500 images\/\n\u2502\u00a0\u00a0 \u251c\u2500\u2500 javascripts\/\n\u2502\u00a0\u00a0 \u2514\u2500\u2500 stylesheets\/\n\u2502\u00a0\u00a0     \u2514\u2500\u2500 style.css\n\u251c\u2500\u2500 routes\/\n\u2502\u00a0\u00a0 \u2514\u2500\u2500 index.js\n\u2514\u2500\u2500 views\/\n    \u251c\u2500\u2500 index.jade\n    \u2514\u2500\u2500 layout.jade\n<\/pre>\n<p>\u305f\u3060\u3057\u3001\u7d14\u7c8b\u306aREST API\u3092\u4f5c\u308b\u306e\u3067\u3042\u308c\u3070\u3001<code>views<\/code>\u3068<code>public<\/code>\u306f\u307e\u305a\u4f7f\u3046\u3053\u3068\u306f\u306a\u3044\u3002<\/p>\n<p>\u3053\u306e\u5148\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u30e2\u30b8\u30e5\u30fc\u30eb\u306f\u3001<code>package.json<\/code>\u3092\u7de8\u96c6\u3057\u3066\u3001<code>npm install<\/code>\u3092\u5b9f\u884c\u3059\u308b\u3068\u3044\u3046\u6d41\u308c\u306b\u306a\u308b\u3002express\u3060\u3051\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u305f\u6bb5\u968e\u3067\u306e<code>package.json<\/code>\u3092\u898b\u3066\u307f\u3088\u3046\u3002<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n{\r\n    &amp;quot;name&amp;quot;: &amp;quot;application-name&amp;quot;\r\n  , &amp;quot;version&amp;quot;: &amp;quot;0.0.1&amp;quot;\r\n  , &amp;quot;private&amp;quot;: true\r\n  , &amp;quot;dependencies&amp;quot;: {\r\n      &amp;quot;express&amp;quot;: &amp;quot;2.5.8&amp;quot;\r\n    , &amp;quot;jade&amp;quot;: &amp;quot;&amp;gt;= 0.0.1&amp;quot;\r\n  }\r\n}\r\n<\/pre>\n<p><code>dependencies<\/code>\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u305f\u3044\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u4ed8\u3051\u8db3\u3057\u3066\u3044\u304f\u308f\u3051\u3060\u3002<\/p>\n<h3>express-resource<\/h3>\n<p><a href=\"https:\/\/github.com\/expressjs\/express-resource\">express-resource<\/a>\u306f\u3001REST API\u306e\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u3092\u63d0\u4f9b\u3057\u3066\u304f\u308c\u308b\u3002<code>app.js<\/code>\u3084<code>routes\/index.js<\/code>\u3067\u3001HTTP\u30e1\u30bd\u30c3\u30c9\u3068URL\u3001\u547c\u3073\u51fa\u3059\u95a2\u6570\u306e\u30de\u30c3\u30d7\u3092\u81ea\u5206\u3067\u66f8\u304b\u306a\u304f\u3066\u6e08\u3080\u308f\u3051\u3060\u3002\u4f8b\u3048\u3070\u3001\u30e6\u30fc\u30b6\u30fc\u60c5\u5831\u3092\u683c\u7d0d\u3057\u305fDB\u304c\u3042\u3063\u3066\u3001\u305d\u308c\u306b\u5bfe\u5fdc\u3059\u308bREST API\u3092\u63d0\u4f9b\u3059\u308b\u5834\u5408\u3001<code>routes\/user.js<\/code>\uff08\u307e\u305f\u306f<code>routes\/user\/index.js<\/code>\uff09\u3092\u4f5c\u3063\u3066\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u66f8\u304f\u3053\u3068\u306b\u306a\u308b\u3002<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nmodule.exports = {\r\n\tindex: function(req, res) {\r\n\t},\r\n\tnew: function(req, res) {\r\n\t},\r\n\tcreate: function(req, res) {\r\n\t},\r\n\tshow: function(req, res) {\r\n\t},\r\n\tedit: function(req, res) {\r\n\t},\r\n\tupdate: function(req, res) {\r\n\t},\r\n\tdestroy: function(req, res) {\r\n\t}\r\n};\r\n<\/pre>\n<p>\u305d\u3057\u3066\u3001<code>app.js<\/code>\u3067\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u8ffd\u52a0\u3059\u308b\u3002<\/p>\n<pre class=\"brush: jscript; highlight: [2,5]; title: ; notranslate\" title=\"\">\r\nvar express = require('express')\r\n  , resource = require('express-resource')\r\n  , app = module.exports = express.createServer();\r\n\r\napp.resource('users', require('.\/routes\/user'), { id: 'id' });\r\n<\/pre>\n<p><code>routes\/user.js<\/code>\u3067\u5b9f\u88c5\u3057\u305f\u30e2\u30b8\u30e5\u30fc\u30eb\u306e\u5404\u30e1\u30bd\u30c3\u30c9\u306f\u3001express-resource\u304c\u4ee5\u4e0b\u306e\u3088\u3046\u306bHTTP\u30e1\u30bd\u30c3\u30c9\u3082\u542b\u3081\u3066\u30de\u30c3\u30d7\u3057\u3066\u304f\u308c\u308b\u3002<\/p>\n<pre>\nGET     \/users              -&gt;  index\nGET     \/users\/new          -&gt;  new\nPOST    \/users              -&gt;  create\nGET     \/users\/:id          -&gt;  show\nGET     \/users\/:id\/edit     -&gt;  edit\nPUT     \/users\/:id          -&gt;  update\nDELETE  \/users\/:id          -&gt;  destroy\n<\/pre>\n<p><code>app.resource()<\/code>\u306e\u7b2c3\u5f15\u6570\u3067\u3001\u30ea\u30bd\u30fc\u30b9\u306e\u30ad\u30fc\u3068\u306a\u308b\u30d7\u30ec\u30fc\u30b9\u30db\u30eb\u30c0\u30fc\u540d\u3092<code>id<\/code>\u3068\u6307\u5b9a\u3057\u3066\u3044\u308b\u305f\u3081\u3001\u4e0a\u8a18\u306e\u3088\u3046\u306a\u30de\u30c3\u30d7\u306b\u306a\u3063\u3066\u3044\u308b\u304c\u3001\u7b2c3\u5f15\u6570\u3092\u7701\u7565\u3059\u308b\u3068\u7b2c1\u5f15\u6570\u306e\u5358\u6570\u5f62\u304c\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u4f7f\u308f\u308c\u308b\u3002\u4f8b\u3048\u3070\u3001<code>user.show()<\/code>\u306b\u5bfe\u5fdc\u3059\u308bURL\u306f\u3001<code>\/users\/:user<\/code>\u306b\u306a\u308b\u308f\u3051\u3060\u3002<\/p>\n<p>express-resource\u306f\u30b3\u30f3\u30c6\u30f3\u30c8\u30cd\u30b4\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u306a\u3093\u3066\u6c17\u306e\u5229\u3044\u305f\u3082\u306e\u307e\u3067\u7528\u610f\u3055\u308c\u3066\u3044\u308b\u3002\u4f8b\u3048\u3070\u3001<code>\/users.json<\/code>\u306b\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u6295\u3052\u308b\u3068JSON\u5f62\u5f0f\u3067\u3001<code>\/users.xml<\/code>\u306b\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u6295\u3052\u308b\u3068XML\u5f62\u5f0f\u3067\u7d50\u679c\u3092\u8fd4\u3057\u3066\u304f\u308c\u308b\u3088\u3046\u306aREST API\u3092\u7c21\u5358\u306b\u63d0\u4f9b\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b\u3002<\/p>\n<p>\u9806\u756a\u304c\u9006\u306b\u306a\u3063\u3066\u3057\u307e\u3063\u305f\u304c\u3001express-resource\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u306b\u306f\u3001packege.json\u3092\u307e\u305a\u306f\u7de8\u96c6\u3002<\/p>\n<pre class=\"brush: jscript; highlight: [7]; title: ; notranslate\" title=\"\">\r\n{\r\n    &amp;quot;name&amp;quot;: &amp;quot;application-name&amp;quot;\r\n  , &amp;quot;version&amp;quot;: &amp;quot;0.0.1&amp;quot;\r\n  , &amp;quot;private&amp;quot;: true\r\n  , &amp;quot;dependencies&amp;quot;: {\r\n      &amp;quot;express&amp;quot;: &amp;quot;2.5.8&amp;quot;\r\n    , &amp;quot;express-resource&amp;quot;: &amp;quot;latest&amp;quot;\r\n    , &amp;quot;jade&amp;quot;: &amp;quot;&amp;gt;= 0.0.1&amp;quot;\r\n  }\r\n}\r\n<\/pre>\n<p>\u3042\u3068\u306f<code>npm install<\/code>\u3092\u5b9f\u884c\u3059\u308b\u3060\u3051\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u300cnode.js\u3092\u4f7f\u3063\u3066\u7c21\u5358\u306bWeb\u30a2\u30d7\u30ea\u304c\u4f5c\u308c\u308b\uff01\u300d\u307f\u305f\u3044\u306a\u8a18\u4e8b\u3067\u7d39\u4ecb\u3055\u308c\u3066\u3044\u308b\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u306f\u3001\u3060\u3044\u305f\u3044\u305d\u306e\u307e\u307e\u4f7f\u3044\u3082\u306e\u306b\u306a\u3089\u306a\u3044\u3053\u3068\u304c\u591a\u3044\u3002\u5b9f\u969b\u306e\u30a2\u30d7\u30ea\u958b\u767a\u3067\u306f\u3001\u3044\u308d\u3044\u308d\u306a\u30e2\u30b8\u30e5\u30fc\u30eb\u3092NPM\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u3001\u30bd\u30fc <a href='https:\/\/h2plus.biz\/hiromitsu\/entry\/617' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[77],"tags":[221,219,222,81,223,220,230],"_links":{"self":[{"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/posts\/617"}],"collection":[{"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/comments?post=617"}],"version-history":[{"count":1,"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/posts\/617\/revisions"}],"predecessor-version":[{"id":992,"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/posts\/617\/revisions\/992"}],"wp:attachment":[{"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/media?parent=617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/categories?post=617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/tags?post=617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}