{"id":1629,"date":"2021-12-31T22:10:37","date_gmt":"2021-12-31T13:10:37","guid":{"rendered":"https:\/\/dalomo.net\/blog\/?p=1629"},"modified":"2021-12-31T22:23:51","modified_gmt":"2021-12-31T13:23:51","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%a4-nodebbs-msg%e3%81%a7%e5%89%8a%e9%99%a4%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/dalomo.net\/blog\/2021\/12\/31\/1629\/","title":{"rendered":"\u7c21\u6613\u63b2\u793a\u677f\u3092\u4f5c\u308a\u305f\u3044\u3001node.js+express\u3067 \u2464\/nodebbs\/msg\u3067\u524a\u9664\u3059\u308b"},"content":{"rendered":"<h1>\u524a\u9664<\/h1>\n<p>\u524a\u9664\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u305f\u3044\u3002\u307b\u3093\u3068\u306f\u30d1\u30b9\u30ef\u30fc\u30c9\u3068\u304b\u5165\u308c\u3066\u66f8\u304d\u8fbc\u3093\u3060\u672c\u4eba\u3067\u3042\u308b\u304b\u306e\u30c1\u30a7\u30c3\u30af\u304c\u5fc5\u8981\u306a\u3093\u3060\u308d\u3046\u3051\u3069\u3001\u307e\u305a\u306f\u30dc\u30bf\u30f3\u4e00\u767a\u5373\u524a\u9664\u3068\u3044\u3046\u5f62\u3067\u4f5c\u308a\u305f\u3044\u3002<\/p>\n<h2>ID\u7684\u306a\u8981\u7d20\u3092\u8ffd\u52a0\u3059\u308b<\/h2>\n<p>\u898b\u901a\u3057\u304c\u7acb\u3063\u3066\u3044\u306a\u3044\u304c\u3042\u308b\u3068\u4fbf\u5229\u305d\u3046\u306a\u306e\u3067\u8ffd\u52a0\u3057\u305f\u3044\u3002\u3069\u306e\u7a0b\u5ea6\u306e\u30b9\u30da\u30c3\u30af\u304c\u5fc5\u8981\u306aID\u304b\u3068\u3044\u3046\u306e\u306f\u306a\u3093\u304b\u8272\u3005\u3042\u308b\u307f\u305f\u3044\u3060\u304c\u6642\u9593\u3067\u3044\u3044\u3084\u3068\u601d\u3063\u305f\u3002<\/p>\n<p><a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Date\/now\">https:\/\/developer.mozilla.org\/ja\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Date\/now<\/a><\/p>\n<p>\u3061\u3085\u30fc\u3053\u3068\u3067app.post\u5185\u3092<\/p>\n<pre><code class=\"js\">var obj = {\r\n    id: Date.now(),\r\n    contents: {\r\n      name: req.body['user_name'],\r\n      msg: req.body['user_message'],\r\n      dt: (new Date()).toJSON()\r\n    }\r\n  }<\/code><\/pre>\n<p>\u3053\u3046\u5909\u3048\u305f\u3002\u305d\u308c\u306b\u4f34\u3063\u3066<\/p>\n<pre> for (let row of data) {\r\n    s += `\r\n        &lt;p&gt;${row['contents']['msg']}&lt;\/p&gt;\r\n        &lt;p&gt;&lt;button type=\"button\" id=\"${row['id']}\" onclick=\"delbtn()\"&gt;\u524a\u9664&lt;\/button&gt; \u540d\u524d:${row['contents']['name']} \u65e5\u6642: ${row['contents']['dt']}&lt;\/p&gt;\r\n        &lt;hr&gt;\r\n        `\r\n    }<\/pre>\n<p>\u3053\u3046\u3057\u305f\u3002<\/p>\n<h2>HTTP DELETE\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3046<\/h2>\n<p>\u30dc\u30bf\u30f3\u3092\u62bc\u3057\u305f\u3089id\u3092\u4f34\u3063\u3066\u30b5\u30fc\u30d0\u30fc\u5074\u306b\u98db\u3070\u3059\u30a4\u30e1\u30fc\u30b8\u3067\u66f8\u304f\u3002<\/p>\n<pre><code class=\"js\">function delbtn() {\r\n            let id = event.target.id\r\n            fetch('https:\/\/dalomo.net\/nodebbs\/msg\/' + id, {\r\n                    method: 'DELETE'\r\n                })\r\n                .then(res =&gt; location.reload())\r\n        }<\/code><\/pre>\n<p>\u3053\u308c\u3001VSC\u4f7f\u3063\u3066\u305f\u3089\u307b\u307c\u66f8\u3044\u3066\u304f\u308c\u305f\u3002\u3044\u3064\u306e\u9593\u306b\u304b\u672a\u6765\u3044\u305f\u308f\u3002<\/p>\n<p>event\u304c\u30af\u30ea\u30c3\u30af\u30a4\u30d9\u30f3\u30c8\u306e\u8981\u7d20\u304c\u5165\u3063\u3068\u308b\u3089\u3057\u3044\u3002\u305d\u3053\u306eid\u3092\u53d6\u5f97<\/p>\n<p><a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Web\/API\/Event\/target\">https:\/\/developer.mozilla.org\/ja\/docs\/Web\/API\/Event\/target<\/a><\/p>\n<p>HTML\u306e\u307f\u3067DELETE\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3046\u65b9\u6cd5\u304c\u308f\u304b\u3089\u306a\u304b\u3063\u305f\u306e\u3067\u3001Javascript\u306efetch\u3092\u4f7f\u3046<\/p>\n<p><a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Web\/API\/Fetch_API\/Using_Fetch#supplying_request_options\">https:\/\/developer.mozilla.org\/ja\/docs\/Web\/API\/Fetch_API\/Using_Fetch#supplying_request_options<\/a><\/p>\n<p>\u305d\u306e\u307e\u307e\u307b\u3063\u3068\u304f\u3068\u753b\u9762\u304c\u66f4\u65b0\u3055\u308c\u306a\u304b\u3063\u305f\u306e\u3067\u30ea\u30ed\u30fc\u30c9\u3092\u5165\u308c\u3066\u307f\u305f\u3002\u3053\u308c\u3082\u3063\u3068\u30b9\u30de\u30fc\u30c8\u306a\u65b9\u6cd5\u306a\u3044\u306e\u304b\u306a\u3002<\/p>\n<h2>\u53d7\u3051\u53d6\u3063\u305f\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u30b5\u30fc\u30d0\u30fc\u3067\u51e6\u7406\u3059\u308b<\/h2>\n<pre><code class=\"js\">app.delete('\/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.filter(item =&gt; item.id != id)\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\u3001\u3053\u308c\u3082\u307b\u307c\u81ea\u52d5\u7684\u306b\u4f5c\u3089\u308c\u305f\u2026\u3002\u3084\u3079\u30fc\u306a\u672a\u6765\u3002\u3067\u3082\u3042\u3093\u307e\u7406\u89e3\u5ea6\u304c\u9ad8\u304f\u306a\u3044\u304b\u3089\u3053\u308c\u304c\u5408\u3063\u3066\u308b\u306e\u304b\u5206\u304b\u3089\u306a\u3044\u3093\u3067\u3059\u3088\u306d\u3002\u3002\u3002<\/p>\n<p>app.delete\u3067DELETE\u30e1\u30bd\u30c3\u30c9\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u51e6\u7406\u3067\u304d\u308b\u3002\u30eb\u30fc\u30c8\u30d1\u30b9\u306b\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u542b\u3081\u3066\u3001req.params.id\u3067\u53d6\u5f97\u3067\u304d\u308b\u3002<\/p>\n<p><a href=\"https:\/\/expressjs.com\/ja\/guide\/routing.html#:~:text=res.send(%27\/.*fly%24\/%27)%0A%7D)-,%E3%83%AB%E3%83%BC%E3%83%88%E3%83%BB%E3%83%91%E3%83%A9%E3%83%A1%E3%83%BC%E3%82%BF,-%E3%83%AB%E3%83%BC%E3%83%88%E3%83%BB%E3%83%91%E3%83%A9%E3%83%A1%E3%83%BC%E3%82%BF%E3%81%AF\">https:\/\/expressjs.com\/ja\/guide\/routing.html#:~:text=res.send(%27\/.*fly%24\/%27)%0A%7D)-,%E3%83%AB%E3%83%BC%E3%83%88%E3%83%BB%E3%83%91%E3%83%A9%E3%83%A1%E3%83%BC%E3%82%BF,-%E3%83%AB%E3%83%BC%E3%83%88%E3%83%BB%E3%83%91%E3%83%A9%E3%83%A1%E3%83%BC%E3%82%BF%E3%81%AF<\/a><\/p>\n<p>JSON\u3092\u8aad\u307f\u8fbc\u3093\u3067\u3001obj.filter(item =&gt; item.id != id)\u306e\u3068\u3053\u308d\u3001\u3053\u308c\u52dd\u624b\u306bAI\u304c\u4f5c\u3063\u305f\u30b3\u30fc\u30c9\u306a\u3093\u3060\u3051\u3069id\u3067\u30d5\u30a3\u30eb\u30bf\u30fc\u304b\u3051\u308b\u3093\u3060\u3051\u3069\u524a\u9664\u3057\u305f\u3044id\u9664\u5916\u3067\u30d5\u30a3\u30eb\u30bf\u304b\u3051\u308b\u3093\u3060\u3088\u306d\u3002\u5168\u7136\u767a\u60f3\u6301\u3063\u3066\u306a\u304b\u3063\u305f\u308f\u3002\u8003\u3048\u3066\u305f\u306e\u306fid\u3067\u306a\u3093\u304b\u691c\u7d22\u3068\u304b\u3057\u3066\u3001\u305d\u306e\u8981\u7d20\u306e\u307f\u524a\u9664\u307f\u305f\u3044\u306a\u611f\u3058\u3067\u8003\u3048\u3066\u305f\u3002\u3059\u3052\u30fc\u3088\u3001\u3053\u3063\u3061\u306e\u304c\u5168\u7136\u30b9\u30de\u30fc\u30c8\u306a\u89e3\u6c7a\u3060\u308f\u3002<\/p>\n<p><a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/filter\">https:\/\/developer.mozilla.org\/ja\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/filter<\/a><\/p>\n<p>\u3067\u3001\u6587\u5b57\u5217\u306b\u5909\u63db\u3057\u3066\u3001\u6700\u521d\u3068\u6700\u5f8c\u306e[]\u3092\u524a\u9664\u3057\u3066\u3001\u30d5\u30a1\u30a4\u30eb\u3092\u66f8\u304d\u51fa\u3057\u3002\u3067res.end()\u3067\u7d42\u4e86\u3002\u3067\u304d\u305f\u30fc\u3002<\/p>\n<p>\u30df\u30ea\u79d2\u5358\u4f4d\u306a\u306e\u3067\u81ea\u5206\u306e\u5834\u5408\u306f\u307b\u307c\u8d77\u3053\u3089\u306a\u3044\u3068\u601d\u3046\u3051\u3069\u3001\u4f8b\u3048\u3070\u524a\u9664\u306e\u305f\u3081\u306b\u8aad\u307f\u8fbc\u3093\u3067\u308b\u9014\u4e2d\u3067\u65b0\u898f\u66f8\u304d\u8fbc\u307f\u304c\u3042\u3063\u305f\u308a\u3059\u308b\u3068\u3001\u524a\u9664\u5f8c\u306e\u30d5\u30a1\u30a4\u30eb\u66f8\u304d\u8fbc\u307f\u306b\u3088\u3063\u3066\u3001\u65b0\u898f\u304c\u7121\u3044\u72b6\u614b\u3067\u306e\u4e0a\u66f8\u304d\u304c\u3055\u308c\u308b\u306e\u3067\u3001\u65b0\u898f\u304c\u3069\u3063\u304b\u3044\u304f\u307f\u305f\u3044\u306a\u3053\u3068\u306f\u8d77\u3053\u308a\u3046\u308b\u3002\u3053\u3046\u3044\u3046\u306e\u3092\u9632\u3050\u305f\u3081\u306bDB\u3068\u304b\u306e\u4ed5\u7d44\u307f\u304c\u3042\u3063\u305f\u308a\u3059\u306e\u304b\u306a\u3041\u3068\u304b\u601d\u3063\u305f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524a\u9664 \u524a\u9664\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u305f\u3044\u3002\u307b\u3093\u3068\u306f\u30d1\u30b9\u30ef\u30fc\u30c9\u3068\u304b\u5165\u308c\u3066\u66f8\u304d\u8fbc\u3093\u3060\u672c\u4eba\u3067\u3042\u308b\u304b\u306e\u30c1\u30a7\u30c3\u30af\u304c\u5fc5\u8981\u306a\u3093\u3060\u308d\u3046\u3051\u3069\u3001\u307e\u305a\u306f\u30dc\u30bf\u30f3\u4e00\u767a\u5373\u524a\u9664\u3068\u3044\u3046\u5f62\u3067\u4f5c\u308a\u305f\u3044\u3002 ID\u7684\u306a\u8981\u7d20\u3092\u8ffd\u52a0\u3059\u308b \u898b\u901a\u3057\u304c\u7acb\u3063\u3066\u3044\u306a\u3044\u304c\u3042\u308b\u3068\u4fbf\u5229\u305d\u3046\u306a &hellip; <a href=\"https:\/\/dalomo.net\/blog\/2021\/12\/31\/1629\/\">\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-1629","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\/1629","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=1629"}],"version-history":[{"count":3,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/posts\/1629\/revisions"}],"predecessor-version":[{"id":1632,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/posts\/1629\/revisions\/1632"}],"wp:attachment":[{"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/media?parent=1629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/categories?post=1629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/tags?post=1629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}