{"id":1634,"date":"2022-01-01T21:15:24","date_gmt":"2022-01-01T12:15:24","guid":{"rendered":"https:\/\/dalomo.net\/blog\/?p=1634"},"modified":"2022-01-01T21:15:24","modified_gmt":"2022-01-01T12:15:24","slug":"%e7%b0%a1%e6%98%93%e6%8e%b2%e7%a4%ba%e6%9d%bf%e3%82%92%e4%bd%9c%e3%82%8a%e3%81%9f%e3%81%84%e3%80%81node-jsexpress%e3%81%a7-%e2%91%a5-nodebbs-msg%e3%81%a7%e6%9b%b4%e6%96%b0%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/dalomo.net\/blog\/2022\/01\/01\/1634\/","title":{"rendered":"\u7c21\u6613\u63b2\u793a\u677f\u3092\u4f5c\u308a\u305f\u3044\u3001node.js+express\u3067 \u2465\/nodebbs\/msg\u3067\u66f4\u65b0\u3059\u308b"},"content":{"rendered":"<h1>\u66f4\u65b0<\/h1>\n<p>\u66f4\u65b0\u6a5f\u80fd\u3092\u3064\u3051\u305f\u3044\u3002\u3069\u3046\u3044\u3046\u611f\u3058\u304c\u3044\u3044\u3093\u3060\u308d\u3046\u306a\u30fc\u3068\u8003\u3048\u3066\u3001\u7de8\u96c6\u30dc\u30bf\u30f3\u3092\u62bc\u3059\u3068\u6295\u7a3f\u3055\u308c\u305f\u6587\u7ae0\u304c\u7de8\u96c6\u3067\u304d\u308b\u3088\u3046\u306b\u5909\u308f\u3063\u3066\u3001\u66f4\u65b0\u30dc\u30bf\u30f3\u3092\u62bc\u3059\u3068\u305d\u306e\u307e\u307e\u66f4\u65b0\u3055\u308c\u308b\u306e\u304c\u304b\u3063\u3053\u3088\u3055\u3081\u3058\u3083\u306a\u3044\u304b\u3068\u601d\u3063\u305f\u3002\u305d\u3046\u3044\u3046\u611f\u3058\u306b\u3067\u304d\u308b\u3088\u3046\u306b\u9811\u5f35\u308b\u3002<\/p>\n<h2>\u30dc\u30bf\u30f3\u3092\u4ed8\u3051\u308b<\/h2>\n<pre>&lt;p id=\"${row['id']}\"&gt;${row['contents']['msg']}&lt;\/p&gt;\r\n&lt;p&gt;&lt;button type=\"button\" id=\"edit-${row['id']}\" onclick=\"editbtn()\"&gt;\u7de8\u96c6&lt;\/button&gt;\r\n\u540d\u524d: ${row['contents']['name']} \u65e5\u6642: ${row['contents']['dt']}\r\n&lt;button type=\"button\" id=\"del-${row['id']}\" onclick=\"delbtn()\"&gt;\u524a\u9664&lt;\/button&gt;&lt;\/p&gt;\r\n&lt;hr&gt;<\/pre>\n<p>\u8868\u793a\u306e\u3068\u3053\u306b\u65b0\u305f\u306b\u30dc\u30bf\u30f3\u3092\u4f5c\u3063\u305f\u3002\u5f8c\u3005\u306e\u305f\u3081\u306b\u3066\u304d\u3068\u30fc\u306bid\u3082\u304f\u3063\u3064\u3051\u3066\u304a\u304f\u3002\u3053\u308c\u3092\u62bc\u3059\u3068\u307e\u305a\u306f\u7de8\u96c6\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u305f\u3044\u3002<\/p>\n<h2>\u30dc\u30bf\u30f3\u3092\u62bc\u3059\u3068\u7de8\u96c6\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b<\/h2>\n<pre><code class=\"js\">function editbtn() {\r\n            let id = event.target.id.slice(5)\r\n            let p = document.getElementById(id)\r\n            let msg = p.innerText\r\n            let textarea = document.createElement('textarea')\r\n            textarea.value = msg\r\n            textarea.id = id\r\n            p.replaceWith(textarea)\r\n\r\n            let btn = document.getElementById('edit-' + id)\r\n            btn.innerText = '\u66f4\u65b0'\r\n            btn.setAttribute('onclick', 'update()');\r\n        }<\/code><\/pre>\n<p>\u5197\u9577\u304b\u306a\u3041\u3002\u3067\u3082\u3053\u3046\u3057\u304b\u601d\u3044\u3064\u304b\u3093\u3002id\u3067&lt;p&gt;\u30bf\u30b0\u306e\u5185\u5bb9\u3092\u8272\u3005\u53d6\u5f97\u3057\u3066\u3001\u65b0\u305f\u306b&lt;textarea&gt;\u306e\u8981\u7d20\u3092\u4f5c\u308areplaceWith\u3067\u7f6e\u304d\u63db\u3048\u305f\u3002<\/p>\n<p><a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Web\/API\/Element\/replaceWith\">https:\/\/developer.mozilla.org\/ja\/docs\/Web\/API\/Element\/replaceWith<\/a><\/p>\n<p>\u305d\u3093\u3067\u30dc\u30bf\u30f3\u306e\u65b9\u306f\u3001\u66f4\u65b0\u30dc\u30bf\u30f3\u306b\u306a\u308b\u3088\u3046\u306b\u30c6\u30ad\u30b9\u30c8\u3068onclick\u306e\u5185\u5bb9\u3092\u5909\u3048\u3066\u3044\u308b\u3002<\/p>\n<p><a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Web\/API\/Element\/setAttribute\">https:\/\/developer.mozilla.org\/ja\/docs\/Web\/API\/Element\/setAttribute<\/a><\/p>\n<h2>\u30dc\u30bf\u30f3\u3092\u62bc\u3059\u3068\u66f4\u65b0\u51e6\u7406\u3059\u308b<\/h2>\n<pre><code class=\"js\">function update() {\r\n            let id = event.target.id.slice(5)\r\n            let textarea = document.getElementById(id)\r\n            let msg = textarea.value\r\n            let data = {\r\n                'msg': msg\r\n            }\r\n            fetch('https:\/\/dalomo.net\/nodebbs\/msg\/' + id, {\r\n                method: 'PUT',\r\n                body: JSON.stringify(data),\r\n                headers: {\r\n                    'Content-Type': 'application\/json'\r\n                }\r\n            })\r\n                .then(res =&gt; {\r\n                    let p = document.createElement('p')\r\n                    p.id = id\r\n                    p.innerText = msg\r\n                    textarea.replaceWith(p)\r\n                })\r\n        }<\/code><\/pre>\n<p>\u3053\u308c\u3082IntelliSense\u3055\u3093\u304c\u6012\u6d9b\u306e\u52e2\u3044\u3067\u4f5c\u3063\u3066\u304f\u308c\u305f\u2026\u3002<\/p>\n<p>testarea\u306b\u5165\u529b\u3055\u308c\u305f\u5024\u3092\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u3057\u3066\u3001fetch\u3067PUT\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3044\u3001body\u306b\u66f4\u65b0\u5185\u5bb9\u3092stringify\u3057\u3066\u9001\u4fe1\u3057\u305f\u3002headers\u306f\u307e\u3041\u5fc5\u8981\u306a\u3093\u3060\u308d\u3046\u3053\u308c\u3002<a href=\"https:\/\/qiita.com\/mightysosuke\/items\/39fd5454b28143d3320a\">\u3053\u3053<\/a>\u3068\u304b\u8aad\u3080\u3068\u3053\u3046\u3059\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u30b5\u30fc\u30d0\u30fc\u5074\u3067\u53d7\u3051\u53d6\u308a\u3084\u3059\u304f\u306a\u3063\u3066\u308b\u306e\u304b\u306a\u3002\u3067\u3001\u30ec\u30b9\u30dd\u30f3\u30b9\u8fd4\u3063\u3066\u304d\u305f\u3089\u3001textarea\u304b\u3089p\u30bf\u30b0\u306b\u623b\u3057\u3066\u7d42\u308f\u308a\u3002\u3053\u308c\u306f\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u30b5\u30a4\u30c9\u306a\u306e\u3067\u3001\u3042\u3068\u306f\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u3092\u4f5c\u308b\u3002<\/p>\n<h2>\u30b5\u30fc\u30d0\u30fc\u5074\u306e\u30c7\u30fc\u30bf\u3092\u66f4\u65b0\u3059\u308b<\/h2>\n<pre><code class=\"js\">app.put('\/nodebbs\/msg\/:id', (req, res) =&gt; {\r\n  const id = req.params.id\r\n  const obj = JSON.parse('[' + fs.readFileSync('log.json', 'utf8') + ']')\r\n  const newObj = obj.map(item =&gt; {\r\n    if (item.id == id) {\r\n      item.contents.msg = req.body['msg']\r\n      item.contents.dt = (new Date()).toJSON()\r\n    }\r\n    return item\r\n  })\r\n  fs.writeFileSync('log.json', JSON.stringify(newObj).slice(1).slice(0, -1), 'utf8')\r\n  res.end()\r\n})<\/code><\/pre>\n<p>\u3053\u308c\u3082(ry \u30de\u30b8\u3067\u3059\u3054\u3044\u3088\u306d\u3002\u8003\u3048\u308b\u3053\u3068\u304c\u307b\u307c\u306a\u3044\u3088\u3002<\/p>\n<p>\u307e\u3041\u30ed\u30b0\u3092\u8aad\u307f\u8fbc\u3093\u3067\u3001obj.map\u3067\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u8981\u7d20\u3092\u306a\u3081\u3066\u3001id\u304c\u4e00\u81f4\u3057\u305f\u3089\u305d\u306e\u5185\u5bb9\u3092\u66f8\u304d\u63db\u3048\u308b\u3002\u3064\u3044\u3067\u306b\u65e5\u4ed8\u3082\u5909\u308f\u308b\u3002\u3053\u306emap\u95a2\u6570\u3063\u3066\u3044\u3046\u306e\u304c\u3059\u3054\u3044\u4fbf\u5229\u306a\u306e\u306d\u3002for\u6587\u3068\u304b\u3067\u3084\u308b\u3068\u3053\u308d\u3060\u3068\u601d\u3046\u3093\u3060\u3051\u3069IntelliSense\u3055\u3093\u69d8\u3005\u3060\u308f\u3002<\/p>\n<p><a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/map\">https:\/\/developer.mozilla.org\/ja\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/map<\/a><\/p>\n<p>\u3042\u3068\u306f\u7de8\u96c6\u5f8c\u306e\u30c7\u30fc\u30bf\u3092log.json\u306b\u66f8\u304d\u623b\u3059\u3060\u3051\u3002\u3059\u3054\u30fc\u3044\u3001\u3042\u3063\u3068\u3044\u3046\u9593\u306b\u3067\u304d\u3061\u3083\u3063\u305f\u3002<\/p>\n<p><a href=\"https:\/\/dalomo.net\/nodebbs\/\">https:\/\/dalomo.net\/nodebbs\/<\/a><\/p>\n<p>\u3068\u308a\u3042\u3048\u305a\u3001\u57fa\u672c\u7684\u306a\u6a5f\u80fd\u3092\u6301\u3064\u63b2\u793a\u677f\u3092\u4f5c\u308c\u305f\u3002\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u7b49\u52a0\u5473\u3057\u3066\u306a\u3044\u306e\u3067\u3061\u3087\u3063\u3068\u30a2\u30ec\u3067\u3059\u304c\u5b09\u3057\u3044\u306d\u3047\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u66f4\u65b0 \u66f4\u65b0\u6a5f\u80fd\u3092\u3064\u3051\u305f\u3044\u3002\u3069\u3046\u3044\u3046\u611f\u3058\u304c\u3044\u3044\u3093\u3060\u308d\u3046\u306a\u30fc\u3068\u8003\u3048\u3066\u3001\u7de8\u96c6\u30dc\u30bf\u30f3\u3092\u62bc\u3059\u3068\u6295\u7a3f\u3055\u308c\u305f\u6587\u7ae0\u304c\u7de8\u96c6\u3067\u304d\u308b\u3088\u3046\u306b\u5909\u308f\u3063\u3066\u3001\u66f4\u65b0\u30dc\u30bf\u30f3\u3092\u62bc\u3059\u3068\u305d\u306e\u307e\u307e\u66f4\u65b0\u3055\u308c\u308b\u306e\u304c\u304b\u3063\u3053\u3088\u3055\u3081\u3058\u3083\u306a\u3044\u304b\u3068\u601d\u3063\u305f\u3002\u305d\u3046\u3044\u3046\u611f\u3058\u306b\u3067\u304d\u308b\u3088 &hellip; <a href=\"https:\/\/dalomo.net\/blog\/2022\/01\/01\/1634\/\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[8],"tags":[132,130,157],"class_list":["post-1634","post","type-post","status-publish","format-standard","hentry","category-8","tag-express","tag-node-js","tag-157"],"_links":{"self":[{"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/posts\/1634","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/comments?post=1634"}],"version-history":[{"count":1,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/posts\/1634\/revisions"}],"predecessor-version":[{"id":1635,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/posts\/1634\/revisions\/1635"}],"wp:attachment":[{"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/media?parent=1634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/categories?post=1634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/tags?post=1634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}