{"id":1167,"date":"2012-07-21T19:33:34","date_gmt":"2012-07-21T10:33:34","guid":{"rendered":"https:\/\/gunmagisgeek.com\/wordpress\/?p=1167"},"modified":"2017-11-30T11:16:25","modified_gmt":"2017-11-30T02:16:25","slug":"post-1167","status":"publish","type":"post","link":"https:\/\/gunmagisgeek.com\/blog\/node-js\/1167","title":{"rendered":"node.js + restify + mongoDB\u3067\u304a\u624b\u8efd JSON Web API \u4f5c\u6210"},"content":{"rendered":"<p><span class=\"removed_link\" title=\"http:\/\/search.npmjs.org\/\">npm registry<\/span> \u3092\u63a2\u3063\u3066\u3044\u305f\u3089\u300crestify\u300d\u3068\u3044\u3046\u4fbf\u5229\u305d\u3046\u306a\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3092\u898b\u3064\u3051\u305f\u306e\u3067\u4f7f\u3063\u3066\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p><a href=\"http:\/\/mcavage.github.com\/node-restify\/\">restify<\/a><br \/>\n\u3000<\/p>\n<p>restify\u306f\u3001REST Web\u30b5\u30fc\u30d3\u30b9\u306e\u69cb\u7bc9\u306b\u7279\u5316\u3057\u305f\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3067\u3059\u3002<br \/>\n<a href=\"http:\/\/expressjs.com\/\">Express<\/a>\u304b\u3089\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u6a5f\u80fd\u3068\u304b\u3092\u7701\u3044\u3066\u3001\u4fbf\u5229\u306a\u6a5f\u80fd\u3092\u4ed8\u3051\u8db3\u3057\u305f\u611f\u3058\u3067\u3059\u3002<\/p>\n<p><strong>restify\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/strong><\/p>\n<pre class=\"lang:sh decode:true \" >$ npm install restify<\/pre>\n<p>&nbsp;<\/p>\n<p>\u3068\u308a\u3042\u3048\u305a\u3001\u516c\u5f0f\u30b5\u30a4\u30c8\u306b\u3042\u308b\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3092\u52d5\u304b\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js decode:true \" title=\"sample.js\" >var restify = require('restify');\n\nfunction respond(req, res, next) {\n  res.send('hello ' + req.params.name);\n}\n\nvar server = restify.createServer();\nserver.get('\/hello\/:name', respond);\nserver.head('\/hello\/:name', respond);\n\nserver.listen(8080, function() {\n  console.log('%s listening at %s', server.name, server.url);\n});<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u5b9f\u884c<\/strong><\/p>\n<pre class=\"lang:sh decode:true \" title=\"\u5b9f\u884c\" >$ node sample.js<\/pre>\n<p><code>http:\/\/\uff1c\u30db\u30b9\u30c8\u540d\uff1e:8080\/hello\/\uff1c\u4efb\u610f\u306e\u540d\u524d\uff1e<\/code><br \/>\n\u30d6\u30e9\u30a6\u30b6\u3067\u4e0a\u8a18\u306eURL\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068<\/p>\n<p><code>hello \uff1c\u4efb\u610f\u306e\u540d\u524d\uff1e<\/code><br \/>\n\u3068\u30ec\u30b9\u30dd\u30f3\u30b9\u304c\u5e30\u3063\u3066\u304d\u307e\u3059\u3002<br \/>\nURL\u306e\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u51e6\u7406\u304c\u7c21\u6f54\u306b\u304b\u3051\u3066\u4fbf\u5229\u3067\u3059\u306d\u3002<\/p>\n<p>&nbsp;<br \/>\n&nbsp;<\/p>\n<p><strong>\u25a0 \u30b2\u30b9\u30c8\u30d6\u30c3\u30af\u306e\u4f5c\u6210<\/strong><\/p>\n<p>\u300crestify\u300d\u3092\u30b0\u30b0\u3063\u3066\u3044\u305f\u3089\u3001restify\u3092\u4f7f\u3063\u3066\u30b2\u30b9\u30c8\u30d6\u30c3\u30af\u3092\u4f5c\u6210\u3059\u308b\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u3092\u898b\u3064\u3051\u305f\u306e\u3067\u3001\u305d\u308c\u3092\u53c2\u8003\u306b\u7c21\u5358\u306a\u30b2\u30b9\u30c8\u30d6\u30c3\u30af\u3092\u4f5c\u3063\u3066\u307f\u307e\u3059\u3002<\/p>\n<p>\u53c2\u8003<br \/>\n<a href=\"https:\/\/cdnjs.com\/libraries\/backbone.js\/tutorials\/nodejs-restify-mongodb-mongoose\">Simple example &#8211; Node.js, Restify, MongoDb and Mongoose<\/a><\/p>\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u306fMongoDB\u3092\u4f7f\u3046\u306e\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3002<br \/>\n\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u65b9\u6cd5\u306f\u4ee5\u4e0b\u3092\u3002<br \/>\n<a href=\"http:\/\/docs.mongodb.org\/manual\/tutorial\/install-mongodb-on-redhat-centos-or-fedora-linux\/\">MongoDB\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/a><\/p>\n<p>node.js\u304b\u3089mongodb\u3078\u30a2\u30af\u30bb\u30b9\u3059\u308b\u305f\u3081\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3002<\/p>\n<pre class=\"lang:sh decode:true \" >$ npm install mongodb\n$ npm install mongoose<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u30fb\u30b2\u30b9\u30c8\u30d6\u30c3\u30af\u306b\u4f7f\u3046Web API\u30b5\u30fc\u30d0\u30fc\u3092\u8a18\u8ff0<\/strong><\/p>\n<pre class=\"lang:js decode:true \" title=\"webapi.js\" >var restify = require('restify');\nvar server = restify.createServer();\nserver.use(restify.bodyParser());\n\nvar mongoose = require('mongoose');\nvar db = mongoose.connect('mongodb:\/\/localhost\/webapi');\nvar Schema = mongoose.Schema;\n\nvar messageSchema = new Schema({\n    name: String,\n    comment:String,\n    date: Date\n});\n\nmongoose.model('message', messageSchema);\nvar Message = mongoose.model('message');\n\nfunction getMessage(req, res, next){\n    res.header(&quot;Access-Control-Allow-Origin&quot;, &quot;*&quot;);\n    res.header(&quot;Access-Control-Allow-Headers&quot;, &quot;X-Requested-With&quot;);\n    Message.find().sort('date',-1).execFind(function(arr, data){\n        res.send(data);\n    });\n}\n\nfunction postMessage(req, res, next){\n    res.header(&quot;Access-Control-Allow-Origin&quot;, &quot;*&quot;);\n    res.header(&quot;Access-Control-Allow-Headers&quot;, &quot;X-Requested-With&quot;);\n    var message = new Message();\n    message.name = req.params.name;\n    message.comment = req.params.comment;\n    message.date = new Date();\n\n    message.save(function(arr, data){\n        res.send(data);\n    });\n}\n\nserver.get('\/message', getMessage);\nserver.post('\/message', postMessage);\n\nserver.listen(8080, function() {\n    console.log('%s listening at %s', server.name, server.url);\n});<\/pre>\n<p>8080:\/message \u306bPOST\u30ea\u30af\u30a8\u30b9\u30c8\u3067name,comment\u3092\u9001\u4fe1\u3059\u308b\u3068mongoDB\u306b\u4fdd\u5b58\u3057\u307e\u3059\u3002<br \/>\n8080\uff1a\/message\u3000\u306bGET\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u4fe1\u3059\u308b\u3068\u3001\u4fdd\u5b58\u3055\u308c\u3066\u3044\u308b\u3059\u3079\u3066\u306e\u30c7\u30fc\u30bf\u3092JSON\u3067\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<p>\u3068\u308a\u3042\u3048\u305a\u30c6\u30b9\u30c8\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<p>&nbsp;<br \/>\n<strong>\u30fb\u30b5\u30fc\u30d0\u30fc\u8d77\u52d5<\/strong><\/p>\n<pre class=\"lang:sh decode:true \" >$ node webapi.js<\/pre>\n<p>&nbsp;<br \/>\n<strong>\u30fb\u66f8\u304d\u8fbc\u307f\u30c6\u30b9\u30c8<\/strong><br \/>\nPOST\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u4fe1\u3057\u3066\u3001\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u66f8\u304d\u8fbc\u307f\u307e\u3059\u3002\u6210\u529f\u3059\u308b\u3068\u66f8\u304d\u8fbc\u3093\u3060\u5185\u5bb9\u304cjson\u3067\u5e30\u3063\u3066\u304d\u307e\u3059\u3002\u5931\u6557\u3059\u308b\u3068\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8\u304cjson\u3067\u5e30\u3063\u3066\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true \" >curl -d &quot;name=test&amp;comment=Hello World!&quot; http:\/\/\uff1c\u30db\u30b9\u30c8\u540d\uff1e:8080\/message\/\n<\/pre>\n<p>&nbsp;<br \/>\n<strong>\u30fb\u8aad\u307f\u8fbc\u307f\u30c6\u30b9\u30c8<\/strong><br \/>\nGET\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u4fe1\u3057\u3066\u3001\u66f8\u304d\u8fbc\u3093\u3060\u5185\u5bb9\u304c\u3061\u3083\u3093\u3068\u5e30\u3063\u3066\u304f\u308b\u304b\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true \" >curl -is http:\/\/localhost:8080\/message\/<\/pre>\n<p>&nbsp;<br \/>\n<strong>\u30fbmongo\u30b7\u30a7\u30eb\u3067\u78ba\u8a8d<\/strong><br \/>\n\u3061\u3083\u3093\u3068\u66f8\u304d\u8fbc\u307e\u308c\u3066\u3044\u308b\u304b\u3001\u4e00\u5fdcmondo\u5074\u3067\u3082\u78ba\u8a8d\u3002<\/p>\n<pre class=\"lang:sh decode:true \" >$ monogo\n> show dbs\nlocal   (empty)\ntest    0.203125GB\nwebapi  0.203125GB\n> use webapi\nswitched to db webapi\n> show collections\nmessages\n>db.messages.find()\n{ &quot;date&quot; : ISODate(&quot;2012-07-21T03:49:31.807Z&quot;), &quot;comment&quot; : &quot;hello World!&quot;, &quot;name&quot; : &quot;test&quot;, &quot;_id&quot; : ObjectId(&quot;500a26cbfd23dcb93c000029&quot;) }<\/pre>\n<p>&nbsp;<br \/>\n\u3072\u3068\u307e\u305a\u3001\u3053\u308c\u3067\u30b2\u30b9\u30c8\u30d6\u30c3\u30af\u7528\u306eWeb API\u304c\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n<p>&nbsp;<br \/>\n&nbsp;<\/p>\n<p><strong>\u30fb\u30b2\u30b9\u30c8\u30d6\u30c3\u30af\u30da\u30fc\u30b8\u3092\u4f5c\u6210<\/strong><br \/>\njQuery\u3068jQuery.Model\u30d7\u30e9\u30b0\u30a4\u30f3\u3092\u4f7f\u3063\u3066\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:xhtml decode:true \" title=\"guestbook.html\" >&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;meta charset=&quot;utf-8&quot; \/&gt;\n&lt;title&gt;Gest Book&lt;\/title&gt;\n&lt;script type=&quot;text\/javascript&quot; src=&quot;http:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.7.2\/jquery.min.js&quot;&gt;&lt;\/script&gt;\n&lt;script type=&quot;text\/javascript&quot; src=&quot;http:\/\/v3.javascriptmvc.com\/jquery\/dist\/jquery.model.min.js&quot;&gt;&lt;\/script&gt;\n\n&lt;script&gt;\nwindow.onload = function(){\n\n    $.Model('Message',{\n      findAll: 'GET http:\/\/\uff1c\u30db\u30b9\u30c8\u540d\uff1e:8080\/message\/',\n      create:  'POST http:\/\/\uff1c\u30db\u30b9\u30c8\u540d\uff1e:8080\/message\/',\n    },{});\n    \n    var write = function(msg){\n        return  $('&lt;p&gt;').model(msg)\n                    .html(&quot;&lt;b&gt;&quot;+msg.name+&quot;&lt;\/b&gt; &quot;+msg.comment)\n                    .prependTo('#message_list');\n    }\n    \n    Message.findAll([],function(msgs){\n        $.each(msgs, function(i,msg){\n                write(msg);\n        });\n    })\n\n    Message.bind('created', function( ev, msg) {\n        write(msg);\n    })\n        \n    $(&quot;#send&quot;).click(function(){\n        var message = new Message({\n            name: $(&quot;#name&quot;).val(),\n            comment:$(&quot;#comment&quot;).val()\n        });\n        message.save();\n    });\n}\n&lt;\/script&gt;\n\n\n&lt;\/head&gt;\n&lt;body&gt;\nnama:\n&lt;input type=&quot;text&quot; id=&quot;name&quot;&gt;&lt;\/input&gt;\ncomment:\n&lt;input type=&quot;text&quot; id=&quot;comment&quot;&gt;&lt;\/input&gt;\n&lt;input type=&quot;button&quot; id=&quot;send&quot; value=&quot;\u9001\u4fe1&quot;&gt;&lt;\/input&gt;\n&lt;hr&gt;\n&lt;div id=&quot;message_list&quot;&gt;&lt;\/div&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<p>&nbsp;<br \/>\n&nbsp;<\/p>\n<p><strong>\u30fb\u30d6\u30e9\u30a6\u30b6\u3067\u78ba\u8a8d<\/strong><br \/>\nguestbook.html\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3066\u3001\u3061\u3083\u3093\u3068\u66f8\u304d\u8fbc\u307f\u304c\u3067\u304d\u308c\u3070\u3067\u304d\u3042\u304c\u308a\u3067\u3059\u3002<br \/>\n\u2193\u3053\u3093\u306a\u611f\u3058<br \/>\n&nbsp;<br \/>\n<a href=\"http:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2012\/07\/WS000014.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1179\" title=\"WS000014\" src=\"http:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2012\/07\/WS000014.jpg\" alt=\"\" width=\"511\" height=\"142\" srcset=\"https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2012\/07\/WS000014.jpg 511w, https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2012\/07\/WS000014-300x83.jpg 300w\" sizes=\"auto, (max-width: 511px) 100vw, 511px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>npm registry \u3092\u63a2\u3063\u3066\u3044\u305f\u3089\u300crest&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1167","post","type-post","status-publish","format-standard","hentry","category-node-js"],"_links":{"self":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/1167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/comments?post=1167"}],"version-history":[{"count":83,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/1167\/revisions"}],"predecessor-version":[{"id":1246,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/1167\/revisions\/1246"}],"wp:attachment":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media?parent=1167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/categories?post=1167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/tags?post=1167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}