{"id":765,"date":"2012-10-14T22:49:14","date_gmt":"2012-10-15T05:49:14","guid":{"rendered":"http:\/\/h2plus.biz\/hiromitsu\/?p=765"},"modified":"2018-08-06T14:40:48","modified_gmt":"2018-08-06T21:40:48","slug":"exprest%e3%82%92npm%e3%81%ab%e3%83%aa%e3%83%aa%e3%83%bc%e3%82%b9","status":"publish","type":"post","link":"https:\/\/h2plus.biz\/hiromitsu\/entry\/765","title":{"rendered":"exprest\u3092npm\u306b\u30ea\u30ea\u30fc\u30b9"},"content":{"rendered":"<p><a href=\"https:\/\/github.com\/expressjs\/express-resource\">express-resource<\/a>\u3092\u4f7f\u3063\u3066\u52b9\u7387\u3088\u304fREST API\u3092\u5b9f\u88c5\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\u305f\u3081\u306eexprest\u3068\u3044\u3046node\u30d1\u30c3\u30b1\u30fc\u30b8\u3092NPM\u3067\u516c\u958b\u3057\u3066\u307f\u305f\u3002\u3068\u8a00\u3063\u3066\u3082\u3001\u30ea\u30ea\u30fc\u30b9\u3057\u305f\u306e\u306f2\u30f6\u6708\u3082\u524d\u306e\u8a71\u3002\u4f7f\u3044\u65b9\u306a\u3069\u306fREADME.md\u306b\u82f1\u8a9e\u3067\u66f8\u3044\u3066\u304a\u3044\u305f\u304c\u3001\u82f1\u8a9e\u304c\u82e6\u624b\u306a\u4eba\u306e\u305f\u3081\u306b\u65e5\u672c\u8a9e\u306e\u5bfe\u8a33\u3092\u66f8\u3044\u3066\u304a\u3053\u3046\u3002<\/p>\n<p><!--more--><\/p>\n<h4>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h4>\n<p>\u901a\u5e38\u306eNPM\u30d1\u30c3\u30b1\u30fc\u30b8\u3068\u540c\u3058\u3088\u3046\u306b<code>npm install<\/code>\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3002<\/p>\n<pre>\n$ npm install exprest\n<\/pre>\n<h4>\u4f7f\u7528\u65b9\u6cd5<\/h4>\n<p><code>exprest<\/code>\u304c\u63d0\u4f9b\u3059\u308b\u306e\u306f<code>configure()<\/code>\u3068\u3044\u3046\u95a2\u6570\u306e\u307f\u3002<\/p>\n<pre>\nexprest.configure(app [,opts]);\n<\/pre>\n<p><code>app<\/code>\u306b\u306f\u3001<code>express.HTTPServer<\/code>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304b<code>express.HTTPSServer<\/code>\u3092\u6e21\u3059\u3088\u3046\u306b\u3002\u3082\u3061\u308d\u3093\u3001<code>exprest#configure()<\/code>\u3092\u547c\u3076\u524d\u306b<code>express-resource<\/code>\u3092<code>require()<\/code>\u3067\u8aad\u307f\u8fbc\u3093\u3067\u304a\u304f\u3053\u3068\u3002<\/p>\n<p>\u4e00\u756a\u30b7\u30f3\u30d7\u30eb\u306a\u4f7f\u3044\u65b9\u306f\u3053\u3093\u306a\u611f\u3058\u3002<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar express = require('express')\r\n  , resource = require('express-resource')\r\n  , exprest = require('exprest')\r\n  , app = express.createServer();\r\n\r\nexprest.configure(app);\r\n<\/pre>\n<p>\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u306e\u30b3\u30fc\u30c9\u306f\u4e00\u5207\u66f8\u304f\u5fc5\u8981\u304c\u306a\u3044\u3002\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30eb\u30fc\u30c8\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b<code>resources<\/code>\u3068\u3044\u3046\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u4f5c\u3063\u3066\u3001\u305d\u3053\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306a<code>resource<\/code>\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u5b9f\u88c5\u3057\u3066\u304a\u3051\u3070\u3001\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u306f<code>exprest<\/code>\u304c\u9762\u5012\u3092\u898b\u3066\u304f\u308c\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u308b\u3002<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nmodule.exports = {\r\n    index: function(req, res) {\r\n      res.end('index');\r\n    }\r\n  , new: function(req, res) {\r\n      res.end('new');\r\n    }\r\n  , create: function(req, res) {\r\n      res.end('create');\r\n    }\r\n  , show: function(req, res) {\r\n      res.end('show');\r\n    }\r\n  , edit: function(req, res) {\r\n      res.end('edit');\r\n    }\r\n  , update: function(req, res) {\r\n      res.end('update');\r\n    }\r\n  , destroy: function(req, res) {\r\n      res.end('destroy');\r\n    }\r\n};\r\n<\/pre>\n<p><code>resource<\/code>\u30e2\u30b8\u30e5\u30fc\u30eb\u306e\u30d5\u30a1\u30a4\u30eb\u540d\u304cREST API\u3067\u63d0\u4f9b\u3059\u308b\u30ea\u30bd\u30fc\u30b9\u3078\u306e\u30d1\u30b9\u3092\u8868\u3059\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u308b\u3002\u4f8b\u3048\u3070\u3001<code>resource\/users.js<\/code>\u3068\u3044\u3046\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u5b9f\u88c5\u3057\u305f\u3089\u3001<code>exprest<\/code>\u304c<code>\/users\/*<\/code>\u306b\u5bfe\u3059\u308b\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u3057\u3066\u304f\u308c\u308b\u3002<\/p>\n<h4>\u30aa\u30d7\u30b7\u30e7\u30f3<\/h4>\n<p><code>configure()<\/code>\u95a2\u6570\u306e\u7b2c2\u5f15\u6570<code>opts<\/code>\u3092\u4f7f\u3063\u3066\u3001<code>exprest<\/code>\u306e\u632f\u308b\u821e\u3044\u3092\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<code>opts<\/code>\u3092\u7701\u7565\u3057\u305f\u5834\u5408\u306f\u3001\u4ee5\u4e0b\u306e\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u304c\u9069\u7528\u3055\u308c\u308b\u3002<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n{\r\n    basedir: 'resources'\r\n  , root: '\/'\r\n  , vars: {}\r\n  , varsKey: 'vars'\r\n  , checkAuth: undefined\r\n  , defaults: {}\r\n}\r\n<\/pre>\n<ul>\n<li><strong>basedir<\/strong>\uff08\u6587\u5b57\u5217\uff09<br \/>\n<code>resource<\/code>\u30e2\u30b8\u30e5\u30fc\u30eb\u306e\u914d\u7f6e\u5148\u3068\u306a\u308b\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3002\n<\/li>\n<li><strong>root<\/strong>\uff08\u6587\u5b57\u5217\uff09<br \/>\nREST API\u306e\u30eb\u30fc\u30c8\u3068\u306a\u308b\u4eee\u60f3\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3002<br \/>\n\u4f8b\u3048\u3070\u3001<code>{ root: '\/api' }<\/code>\u3092\u6e21\u3059\u3068<code>\/api\/users\/*<\/code>\u3078\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u306f<code>resources\/users.js<\/code>\u306b\u30de\u30c3\u30d7\u3055\u308c\u308b\u3002\n<\/li>\n<li><strong>vars<\/strong>\uff08\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09<br \/>\n<a href=\"https:\/\/nodejs.org\/api\/http.html#http_class_http_serverrequest\">http.ServerRequest<\/a>\u3092\u4ecb\u3057\u3066API\u306b\u6e21\u3059\u5909\u6570\u3002<br \/>\n\u3053\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u306b\u3088\u308a\u3001<code>resource<\/code>\u30e2\u30b8\u30e5\u30fc\u30eb\u9593\u3067\u5909\u6570\u3092\u5171\u6709\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002\u4f8b\u3048\u3070\u3001<code>app.js<\/code>\u5185\u3067<code>exprest#configure()<\/code>\u3092\u547c\u3073\u51fa\u3059\u30b3\u30fc\u30c9\u304c\u3042\u3063\u3066\u3001<code>app.js<\/code>\u306e\u30ed\u30fc\u30ab\u30eb\u5909\u6570\u3092<code>resource<\/code>\u30e2\u30b8\u30e5\u30fc\u30eb\u306b\u5f15\u304d\u6e21\u3057\u305f\u304b\u3063\u305f\u3068\u3057\u3088\u3046\u3002\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\uff08<code>GLOBAL<\/code>\uff09\u3092\u4f7f\u3046\u3068\u3044\u3046\u624b\u3082\u3042\u308b\u304c\u3001\u3069\u306e\u3088\u3046\u306a\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u3082\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u306f\u3067\u304d\u308b\u3060\u3051\u907f\u3051\u305f\u3044\u3082\u306e\u3067\u3042\u308b\u3002<code>vars<\/code>\u30aa\u30d7\u30b7\u30e7\u30f3\u306f\u3001\u3053\u306e\u3088\u3046\u306a\u5909\u6570\u3092http.ServerRequest\u7d4c\u7531\u3067\u5f15\u304d\u6e21\u3059\u305f\u3081\u306e\u3082\u306e\u3060\u3002<\/p>\n<p>\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3092\u898b\u308b\u306e\u304c\u308f\u304b\u308a\u3084\u3059\u3044\u3060\u308d\u3046\u3002<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/\/ app.js\r\nvar express = require('express')\r\n  , resource = require('express-resource')\r\n  , exprest = require('exprest')\r\n  , mysql = require('mysql').createConnection(...)\r\n  , app = express.createServer();\r\n\r\nexprest.configure(app, {\r\n    vars: { mysql: mysql }\r\n});\r\n\r\n\/\/ resources\/users.js\r\nmodule.exports = {\r\n    index: function(req, res) {\r\n      var mysql = req.vars.mysql; \/\/ Passed by exprest\r\n      mysql.query('SELECT * FROM users', function(err, users) {\r\n          ...\r\n      });\r\n    }\r\n};\r\n<\/pre>\n<p><code>resource<\/code>\u30e2\u30b8\u30e5\u30fc\u30eb\uff08\u30b5\u30f3\u30d7\u30eb\u3067\u306f<code>users<\/code>\u30e2\u30b8\u30e5\u30fc\u30eb\uff09\u304cDB\u64cd\u4f5c\u3092\u4f34\u3046\u3088\u3046\u306a\u5834\u5408\u3001DB\u63a5\u7d9a\u3092\u4fdd\u6301\u3059\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff08\u30b5\u30f3\u30d7\u30eb\u3067\u306f<code>app.js<\/code>\u5185\u306e<code>mysql<\/code>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09\u3092\u3059\u3079\u3066\u306e<code>resource<\/code>\u30e2\u30b8\u30e5\u30fc\u30eb\u3067\u5171\u6709\u3057\u305f\u3044\u30b1\u30fc\u30b9\u306f\u3088\u304f\u3042\u308b\u3002<code>resource<\/code>\u30e2\u30b8\u30e5\u30fc\u30eb\u306f<code>app.js<\/code>\u3068\u306f\u5225\u30d5\u30a1\u30a4\u30eb\u306b\u5b9f\u88c5\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3001<code>mysql<\/code>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092<code>app.js<\/code>\u304b\u3089\u76f4\u63a5\u7684\u306b<code>resource<\/code>\u30e2\u30b8\u30e5\u30fc\u30eb\u306b\u5f15\u304d\u6e21\u3059\u3053\u3068\u306f\u3067\u304d\u306a\u3044\u304c\u3001<code>exprest.configure()<\/code>\u3067<code>vars<\/code>\u30aa\u30d7\u30b7\u30e7\u30f3\u306b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u5f15\u304d\u6e21\u3059\u3068\u3001<code>resource<\/code>\u30e2\u30b8\u30e5\u30fc\u30eb\u306e\u5404\u30a2\u30af\u30b7\u30e7\u30f3\u30e1\u30bd\u30c3\u30c9\u306e\u5f15\u6570<code>req.vars<\/code>\u3092\u4ecb\u3057\u3066\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b\u3002\n<\/li>\n<li><strong>varsKey<\/strong>\uff08\u6587\u5b57\u5217\uff09<br \/>\n<code>vars<\/code>\u3067\u6e21\u3057\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u3001http.ServerRequest\u306b\u30de\u30c3\u30d7\u3059\u308b\u969b\u306e\u30ad\u30fc\u3092\u6307\u5b9a\u3059\u308b\u3002\u4f8b\u3048\u3070\u3001<code>varsKey<\/code>\u306b<code>\"hoge\"<\/code>\u3092\u6307\u5b9a\u3057\u305f\u5834\u5408\u3001<code>resource<\/code>\u30e2\u30b8\u30e5\u30fc\u30eb\u306e\u5404\u30a2\u30af\u30b7\u30e7\u30f3\u30e1\u30bd\u30c3\u30c9\u3067\u306f\u3001<code>req.hoge<\/code>\u3092\u4ecb\u3057\u3066\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3053\u3068\u306b\u306a\u308b\u3002\n<\/li>\n<li><strong>checkAuth<\/strong>\uff08\u95a2\u6570\uff09<br \/>\n<code>checkAuth<\/code>\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u6307\u5b9a\u3059\u308b\u3068\u3001exprest\u306f\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u306b\u5148\u7acb\u3063\u3066<code>checkAuth<\/code>\u306b\u6e21\u3055\u308c\u305f\u95a2\u6570\u3092\u547c\u3073\u51fa\u3059\u3088\u3046\u306b\u306a\u308b\u3002\u3053\u308c\u306b\u3088\u308a\u3001API\u306b\u5bfe\u3059\u308b\u8a8d\u8a3c\u30d5\u30a3\u30eb\u30bf\u30fc\u3092\u8a2d\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<code>checkAuth<\/code>\u3067\u6307\u5b9a\u3059\u308b\u95a2\u6570\u306f\u3001\u73fe\u5728\u306e\u30bb\u30c3\u30b7\u30e7\u30f3\u304c\u8a8d\u8a3c\u6e08\u307f\u304b\u3069\u3046\u304b\u3092\u793a\u3059\u771f\u507d\u5024\u3092\u8fd4\u3059\u3088\u3046\u306b\u5b9f\u88c5\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<\/p>\n<p><code>checkAuth<\/code>\u304c<code>true<\/code>\u3092\u8fd4\u3057\u305f\u5834\u5408\u3001exprest\u306f\u305d\u306e\u307e\u307eAPI\u306b\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u3059\u308b\u3002<code>false<\/code>\u3092\u8fd4\u3057\u305f\u5834\u5408\u3001exprest\u306f\u4f55\u3082\u3057\u306a\u3044\u3002<code>checkAuth<\/code>\u306f<code>false<\/code>\u3092\u8fd4\u3059\u524d\u306b\u3001\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306b\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u9001\u3089\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<\/p>\n<p>\u4ee5\u4e0b\u306f\u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u7528\u3044\u3066\u8a8d\u8a3c\u72b6\u614b\u3092\u7ba1\u7406\u3059\u308b\u30b5\u30f3\u30d7\u30eb\u3002<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar express = require('express')\r\n  , resource = require('express-resource')\r\n  , exprest = require('exprest')\r\n  , app = express.createServer();\r\n\r\nexprest.configure(app, {\r\n    checkAuth: function(req, res) {\r\n      if(req.session.user_id) {\r\n        return true;\r\n      }\r\n      res.send(403);\r\n      return false;\r\n    }\r\n});\r\n<\/pre>\n<\/li>\n<li><strong>defaults<\/strong>\uff08\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09<br \/>\nexpress-resource\u306b\u5f15\u304d\u6e21\u3059\u30aa\u30d7\u30b7\u30e7\u30f3\u3002\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>express-resource\u3092\u4f7f\u3063\u3066\u52b9\u7387\u3088\u304fREST API\u3092\u5b9f\u88c5\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\u305f\u3081\u306eexprest\u3068\u3044\u3046node\u30d1\u30c3\u30b1\u30fc\u30b8\u3092NPM\u3067\u516c\u958b\u3057\u3066\u307f\u305f\u3002\u3068\u8a00\u3063\u3066\u3082\u3001\u30ea\u30ea\u30fc\u30b9\u3057\u305f\u306e\u306f2\u30f6\u6708\u3082\u524d\u306e\u8a71\u3002\u4f7f\u3044\u65b9\u306a\u3069\u306fREADM <a href='https:\/\/h2plus.biz\/hiromitsu\/entry\/765' 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":[219,222,254,81,220],"_links":{"self":[{"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/posts\/765"}],"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=765"}],"version-history":[{"count":2,"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/posts\/765\/revisions"}],"predecessor-version":[{"id":1008,"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/posts\/765\/revisions\/1008"}],"wp:attachment":[{"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/media?parent=765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/categories?post=765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/h2plus.biz\/hiromitsu\/wp-json\/wp\/v2\/tags?post=765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}