{"id":5580,"date":"2022-11-09T17:41:35","date_gmt":"2022-11-09T08:41:35","guid":{"rendered":"https:\/\/kumapress.com\/?p=5580"},"modified":"2022-11-09T17:42:17","modified_gmt":"2022-11-09T08:42:17","slug":"reactnodejsaxios%e3%81%a7%e7%94%bb%e5%83%8f%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%82%92%e3%82%a2%e3%83%83%e3%83%97%e3%83%ad%e3%83%bc%e3%83%89%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/kumapress.com\/?p=5580","title":{"rendered":"React+Nodejs+axios\u3067\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u3092\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b"},"content":{"rendered":"\n<p>React\u3067\u958b\u767a\u3092\u3057\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u3092Nodejs\u30b5\u30fc\u30d0\u30fc\u306b\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u306b\u306f\u3069\u3046\u3059\u308b\u304b\u3002<br \/>\u7d50\u5c40\u306f\u3001Form\u3092\u4f5c\u6210\u3057\u3066\u3001append\u3059\u308b\u306e\u304c\u4e00\u822c\u7684\u306a\u3088\u3046\u3067\u3059(^^;<br \/>\u203b\u3053\u308c\u306f<a href=\"https:\/\/dev.to\/przpiw\/file-upload-with-react-nodejs-2ho7\" target=\"_blank\" rel=\"noopener\">\u300cFile upload with React &amp; NodeJS\u300d<\/a>\u3092\u53c2\u8003\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u69cb\u6210<\/h3>\n\n\n<p>\u4eca\u56de\u306f\u3053\u3093\u306a\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u69cb\u6210\u306b\u3057\u307e\u3059\u3002<\/p>\n<p>dev<br \/>\u3000frontend &#8212;- React\u74b0\u5883<br \/>\u3000backend &#8212;- Nodejs\u74b0\u5883<br \/>\u3000\u3000images &#8212; \u753b\u50cf\u4fdd\u5b58\u9818\u57df<\/p>\n\n\n<h3 class=\"wp-block-heading\">\u30d5\u30ed\u30f3\u30c8\u5074(React)\u306e\u69cb\u7bc9<\/h3>\n\n\n<p>\u30fbReact\u74b0\u5883\u3092\u69cb\u7bc9\u3057\u307e\u3059\u3002<br \/>$ npx create-react-app frontend<\/p>\n<p>\u30fbaxios\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<br \/>\uff04 npm install axios &#8211;save<\/p>\n<p>\u30fbApp.js\u3092\u66f8\u304d\u63db\u3048\u307e\u3059\u3002<\/p>\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>import '.\/App.css';\nimport React, { useState, useEffect  } from 'react';\nimport axios from 'axios';\n\nfunction App() {\n    const &#091;image, setImage] = useState({ preview: '', data: '' })\n    const &#091;status, setStatus] = useState('')\n    const handleSubmit = async (e) => {\n        e.preventDefault();\n        let formData = new FormData();\n        try {\n            formData.append('file1', image.data);\n            const res = axios.post( 'http:\/\/localhost:5000\/image', formData );\n        } catch( error ) {\n            setStatus( error )\n        }\n    }\n\n    const handleFileChange = (e) => {\n        const img = {\n            preview: URL.createObjectURL(e.target.files&#091;0]),\n            data: e.target.files&#091;0],\n        }\n        setImage(img)\n    }\n\n    return (\n        &lt;div className=\"App\">\n            &lt;h1>Upload to server&lt;\/h1>\n            {image.preview &amp;&amp; &lt;img src={image.preview} width='100' height='100' \/>}\n            &lt;hr>&lt;\/hr>\n            &lt;form onSubmit={handleSubmit}>\n                &lt;input type='file' name='file' onChange={handleFileChange}>&lt;\/input>\n                &lt;button type='submit'>Submit&lt;\/button>\n            &lt;\/form>\n            {status &amp;&amp; &lt;h4>{status}&lt;\/h4>}\n        &lt;\/div>\n    );\n}\n\nexport default App;<\/code><\/pre>\n\n\n\n<p>\u30fb\u8d77\u52d5\u3057\u307e\u3059\u3002<br \/>$ npm start<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-vk-image-border\"><img loading=\"lazy\" decoding=\"async\" width=\"311\" height=\"97\" src=\"https:\/\/kumapress.com\/wp\/wp-content\/uploads\/2022\/11\/ea0bea5e3f3e385a04919b4a8e4f7b82.png\" alt=\"\" class=\"wp-image-5584\" srcset=\"https:\/\/kumapress.com\/wp\/wp-content\/uploads\/2022\/11\/ea0bea5e3f3e385a04919b4a8e4f7b82.png 311w, https:\/\/kumapress.com\/wp\/wp-content\/uploads\/2022\/11\/ea0bea5e3f3e385a04919b4a8e4f7b82-300x94.png 300w\" sizes=\"auto, (max-width: 311px) 100vw, 311px\" \/><\/figure>\n\n\n<p>\u753b\u50cf\u3092\u9078\u629e\u3059\u308b\u3068\u3053\u3093\u306a\u611f\u3058\u3002\u3002<\/p>\n\n\n<figure class=\"wp-block-image size-full is-style-vk-image-border\"><img loading=\"lazy\" decoding=\"async\" width=\"323\" height=\"169\" src=\"https:\/\/kumapress.com\/wp\/wp-content\/uploads\/2022\/11\/ee359b38abcc2fc502210f64ea9370c9.png\" alt=\"\" class=\"wp-image-5585\" srcset=\"https:\/\/kumapress.com\/wp\/wp-content\/uploads\/2022\/11\/ee359b38abcc2fc502210f64ea9370c9.png 323w, https:\/\/kumapress.com\/wp\/wp-content\/uploads\/2022\/11\/ee359b38abcc2fc502210f64ea9370c9-300x157.png 300w\" sizes=\"auto, (max-width: 323px) 100vw, 323px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u30b5\u30fc\u30d0\u30fc\u5074(Nodejs)\u306e\u69cb\u7bc9<\/h3>\n\n\n\n<p>\u30fb\u30b5\u30fc\u30d0\u30fc\u74b0\u5883\u3092\u69cb\u7bc9\u3057\u307e\u3059\u3002<br \/>$ npm install express cors multer<\/p>\n<p>\u30fbapp.js\u3092\u66f8\u304d\u63db\u3048\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>const express = require('express')\nconst app = express()\nconst port = 5000\nconst cors = require('cors')\nconst multer = require('multer')\n\nconst storage = multer.diskStorage({\n    destination: (req, file, cb) => {\n        cb(null, 'images\/')\n    },\n    filename: (req, file, cb) => {\n        cb(null, file.originalname)\n    },\n})\n\nconst upload = multer({ storage: storage })\n\napp.use(cors())\n\napp.post('\/image', upload.single('file1'), function (req, res) {\n    console.log( \"req.file.filename    :\" + req.file.filename );\n    console.log( \"req.file.originalname:\" + req.file.originalname );\n    console.log( \"req.file.mimetype    :\" + req.file.mimetype );\n    res.json({})\n})\n\napp.listen(port, () => {\n  console.log(`listening at http:\/\/localhost:${port}`)\n})<\/code><\/pre>\n\n\n<p>\u30fb\u8d77\u52d5\u3057\u307e\u3059\u3002<br \/>$ node app.js<\/p>\n\n\n<h3 class=\"wp-block-heading\">\u78ba\u8a8d\u3057\u307e\u3059<\/h3>\n\n\n<p>\u30d5\u30a1\u30a4\u30eb\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044(^^)<\/p><aside class=\"row veu_insertAds after\"><div class=\"col-md-6\"><script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\r\n<!-- \u7b2c 1 \u9762 - 2\uff08www.kumapress.com\uff09 -->\r\n<ins class=\"adsbygoogle\"\r\n     style=\"display:block\"\r\n     data-ad-client=\"ca-pub-5987681888830890\"\r\n     data-ad-slot=\"5379270693\"\r\n     data-ad-format=\"auto\"\r\n     data-full-width-responsive=\"true\"><\/ins>\r\n<script>\r\n     (adsbygoogle = window.adsbygoogle || []).push({});\r\n<\/script><\/div><div class=\"col-md-6\"><script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\r\n<!-- \u7b2c 1 \u9762 - 1\uff08www.kumapress.com\uff09 -->\r\n<ins class=\"adsbygoogle\"\r\n     style=\"display:block\"\r\n     data-ad-client=\"ca-pub-5987681888830890\"\r\n     data-ad-slot=\"7353370294\"\r\n     data-ad-format=\"auto\"\r\n     data-full-width-responsive=\"true\"><\/ins>\r\n<script>\r\n     (adsbygoogle = window.adsbygoogle || []).push({});\r\n<\/script><\/div><\/aside>","protected":false},"excerpt":{"rendered":"<p>React\u3067\u958b\u767a\u3092\u3057\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u3092Nodejs\u30b5\u30fc\u30d0\u30fc\u306b\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u306b\u306f\u3069\u3046\u3059\u308b\u304b\u3002\u7d50\u5c40\u306f\u3001Form\u3092\u4f5c\u6210\u3057\u3066\u3001append\u3059\u308b\u306e\u304c\u4e00\u822c\u7684\u306a\u3088\u3046\u3067\u3059(^^;\u203b\u3053\u308c\u306f\u300cFile upload with  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"vkexunit_cta_each_option":"","footnotes":""},"categories":[143,126],"tags":[],"class_list":["post-5580","post","type-post","status-publish","format-standard","hentry","category-node-js","category-blog-memo"],"_links":{"self":[{"href":"https:\/\/kumapress.com\/index.php?rest_route=\/wp\/v2\/posts\/5580","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kumapress.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kumapress.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kumapress.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kumapress.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5580"}],"version-history":[{"count":6,"href":"https:\/\/kumapress.com\/index.php?rest_route=\/wp\/v2\/posts\/5580\/revisions"}],"predecessor-version":[{"id":5588,"href":"https:\/\/kumapress.com\/index.php?rest_route=\/wp\/v2\/posts\/5580\/revisions\/5588"}],"wp:attachment":[{"href":"https:\/\/kumapress.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5580"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kumapress.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5580"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kumapress.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}