{"id":1621,"date":"2021-12-30T12:25:32","date_gmt":"2021-12-30T03:25:32","guid":{"rendered":"https:\/\/dalomo.net\/blog\/?p=1621"},"modified":"2021-12-30T13:02:53","modified_gmt":"2021-12-30T04:02:53","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%a2-nodebbs-msg%e3%81%a7%e6%8a%95%e7%a8%bf%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/dalomo.net\/blog\/2021\/12\/30\/1621\/","title":{"rendered":"\u7c21\u6613\u63b2\u793a\u677f\u3092\u4f5c\u308a\u305f\u3044\u3001node.js+express\u3067 \u2462\/nodebbs\/msg\u3067\u6295\u7a3f\u3059\u308b"},"content":{"rendered":"<h1>\u6295\u7a3f<\/h1>\n<p>\u9001\u4fe1\u30dc\u30bf\u30f3\u3092\u62bc\u3057\u305f\u3089\u5165\u529b\u3055\u308c\u305f\u6587\u5b57\u3092\u4fdd\u5b58\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u3053\u3046\u3002<\/p>\n<h2>POST\u3067\u9001\u3089\u308c\u3066\u304d\u305f\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b<\/h2>\n<p>index.html\u306eform\u306eaction\u306bmsg\u3068\u3057\u3068\u304f\u3002<\/p>\n<div>\n<pre>&lt;form action=\"msg\" method=\"POST\"&gt;<\/pre>\n<\/div>\n<p>app.js\u306f<\/p>\n<pre><code class=\"js\">app.post('\/nodebbs\/msg', (req, res) =&gt; {\r\n\r\n})<\/code><\/pre>\n<p>\u3067\u53d6\u5f97\u3067\u304d\u308b\u3002<\/p>\n<p><a href=\"https:\/\/expressjs.com\/ja\/4x\/api.html#app.post.method\">https:\/\/expressjs.com\/ja\/4x\/api.html#app.post.method<\/a><\/p>\n<p>req.body\u306e\u4e2d\u306b\u5165\u3063\u3066\u308b\u305d\u3046\u3002<\/p>\n<p><a href=\"https:\/\/expressjs.com\/ja\/4x\/api.html#req.body\">https:\/\/expressjs.com\/ja\/4x\/api.html#req.body<\/a><\/p>\n<p>\u6271\u3046\u305f\u3081\u306b\u4ee5\u4e0b2\u884c\u8ffd\u52a0<\/p>\n<div>\n<pre>app.use(express.json())\r\napp.use(express.urlencoded({ extended: true }))<\/pre>\n<\/div>\n<p><a href=\"https:\/\/expressjs.com\/ja\/4x\/api.html#express.json\">https:\/\/expressjs.com\/ja\/4x\/api.html#express.json<\/a><\/p>\n<p><a href=\"https:\/\/expressjs.com\/ja\/4x\/api.html#express.urlencoded\">https:\/\/expressjs.com\/ja\/4x\/api.html#express.urlencoded<\/a><\/p>\n<p>\u305d\u3093\u3067app.post\u306e\u4e2d\u8eab\u3092<\/p>\n<pre> console.log(req.body)<\/pre>\n<p>\u30d5\u30a9\u30fc\u30e0\u304b\u3089\u306a\u3093\u304b\u5165\u529b\u3057\u3066\u9001\u4fe1\u3059\u308b\u3068\u30b3\u30f3\u30bd\u30fc\u30eb\u306b\u8868\u793a\u3055\u308c\u305f\uff01<\/p>\n<h2>\u30c7\u30fc\u30bf\u3092\u30d5\u30a1\u30a4\u30eb\u306b\u4fdd\u5b58<\/h2>\n<p>\u30d5\u30a1\u30a4\u30eb\u5f62\u5f0f\u4f55\u306b\u3057\u3088\u3046\u304b\u306a\u3068\u601d\u3063\u305f\u3093\u3067\u3059\u304c\u3001JSON\u3067\u3044\u30fc\u304b\u306a\u3068\u601d\u3063\u305f\u3002\u306a\u306e\u3067log.json\u3068\u3044\u3046\u7a7a\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u3063\u3066\u304a\u304f\u3002<\/p>\n<p>Node.js\u306b\u306f\u306f\u3058\u3081\u304b\u3089fs\u30e2\u30b8\u30e5\u30fc\u30eb\u3068\u3044\u3046\u30d5\u30a1\u30a4\u30eb\u5165\u51fa\u529b\u7528\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u304c\u7d44\u307f\u8fbc\u307e\u308c\u3066\u3044\u308b\u305d\u3046\u306a\u306e\u3067\u305d\u308c\u3092\u4f7f\u3046\u3002<\/p>\n<pre><code class=\"js\">const fs = require('fs');<\/code><\/pre>\n<p><a href=\"https:\/\/nodejs.org\/api\/fs.html\">https:\/\/nodejs.org\/api\/fs.html<\/a><\/p>\n<p>\u3046\u30fc\u30fc\u30fc\u3093\u3068\u3001\u53d6\u5f97\u3057\u305f\u30c7\u30fc\u30bf\u3092\u4e00\u65e6\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u3057\u3066\u3001JSON\u5f62\u5f0f\u3067\u66f8\u304d\u51fa\u3057\u3066\u3001\u30b5\u30fc\u30d0\u30fc\u30ed\u30fc\u30ab\u30eb\u306e\u30d5\u30a1\u30a4\u30eb\u306b\u8ffd\u8a18\u3059\u308b\u307f\u305f\u3044\u306a\u611f\u3058\u3067\u3069\u3046\u3060\u308d\u3046\u3002<\/p>\n<pre>var obj = { name: req.body['user_name'], msg: req.body['user_message'], dt: (new Date()).toJSON() }<\/pre>\n<p>\u3053\u3093\u306a\u3002\u540d\u524d\u3068\u5185\u5bb9\u3068\u4eca\u306e\u6642\u9593\u3002\u3053\u308c\u3067\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u3067\u304d\u305f\u3002\u3053\u308c\u3092JSON\u6587\u5b57\u5217\u306b\u5909\u63db\u3059\u308b\u3002<\/p>\n<div>\n<pre>var str = ',' + JSON.stringify(obj)<\/pre>\n<\/div>\n<p>\u3053\u306e\u307e\u307e\u3060\u3068&#8221;,&#8221;\u304c\u8db3\u308a\u306a\u3044\u306e\u3067\u524d\u306b\u3064\u3051\u3068\u304f\u3002\u305d\u3057\u305f\u3089\u30d5\u30a1\u30a4\u30eb\u306b\u8ffd\u8a18<\/p>\n<pre>fs.appendFileSync('log.json',str,'utf8')<\/pre>\n<p>\u66f8\u304d\u8fbc\u3081\u305f\uff01\u3067\u3001\u306a\u3093\u304b\u30a4\u30ad\u3063\u3071\u306a\u3057\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u306e\u3067\u3001\u6700\u5f8c\u306b<\/p>\n<pre> res.redirect('\/nodebbs')<\/pre>\n<p>\u3068\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3057\u305f\u3002\u3042\u3068\u306f\u8aad\u307f\u8fbc\u307f\u304b\u306a\u30fc\uff1f<\/p>\n<h2>\u53c2\u8003<\/h2>\n<p><a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Date\/toJSON\">https:\/\/developer.mozilla.org\/ja\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Date\/toJSON<\/a><\/p>\n<p><a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Learn\/JavaScript\/Objects\/JSON\">https:\/\/developer.mozilla.org\/ja\/docs\/Learn\/JavaScript\/Objects\/JSON<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6295\u7a3f \u9001\u4fe1\u30dc\u30bf\u30f3\u3092\u62bc\u3057\u305f\u3089\u5165\u529b\u3055\u308c\u305f\u6587\u5b57\u3092\u4fdd\u5b58\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u3053\u3046\u3002 POST\u3067\u9001\u3089\u308c\u3066\u304d\u305f\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b index.html\u306eform\u306eaction\u306bmsg\u3068\u3057\u3068\u304f\u3002 &lt;form action=&#8221;msg&#8221;  &hellip; <a href=\"https:\/\/dalomo.net\/blog\/2021\/12\/30\/1621\/\">\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-1621","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\/1621","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=1621"}],"version-history":[{"count":4,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/posts\/1621\/revisions"}],"predecessor-version":[{"id":1626,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/posts\/1621\/revisions\/1626"}],"wp:attachment":[{"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/media?parent=1621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/categories?post=1621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/tags?post=1621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}