1つのjsファイルで、httpサーバーを立て、フォームから静的ファイルを送信して、サーバーのコンソール上で解析するようなシステムにしたいのですが、エラーが出てしまいうまく行きません
ネットで調べて試した事としては、jsonファイルがないからエラーが出ていると書いてあったので。
npm init でjsonファイルを作成したのですが、エラーのままでした。
完全に手詰まり状態になってしまったのでお力を貸してください。
js
1var http = require('http'); 2var formidable = require('formidable');//アップロードされたファイルを解析するモジュール 3var fs = require('fs'); 4 5http.createServer(function (req, res) { 6 if (req.url == '/fileupload') {//もしuploadされたら 7 var form = new formidable.IncomingForm();//インスタンス化 8 form.parse(req, function (err, fields, files) {//解析開始(filesにpathが入っている) 9 var oldpath = files.filetoupload.path; 10 var newpath = 'C:/Users/motegiyuuta/' + files.filetoupload.name; 11 fs.rename(oldpath, newpath, function (err) { 12 if (err) throw err; 13 res.write('File uploaded and moved!'); 14 res.end(); 15 }); 16 }); 17} else {//最初に画面 18 res.writeHead(200, {'Content-Type': 'text/html'}); 19 res.write('<form action="fileupload" method="post" enctype="multipart/form-data">'); 20 res.write('<input type="file" name="filetoupload"><br>'); 21 res.write('<input type="submit">'); 22 res.write('</form>'); 23 return res.end(); 24 } 25}).listen(8080); 26console.log("server start!") ; 27
エラーは、
Error: ENOENT: no such file or directory, rename '/var/folders/kd/7v0d2bd50kqbk18b88wbyvn00000gn/T/upload_
0feef12b8cd3fa37468cfcc4c2eb1b0d' -> 'C:/Users/motegiyuuta/スクリーンシ
;ョット 2018-09-15 21.35.32.png'
エラー無くサーバーはスタートされるのですが、
フォームにファイルを添付して送信を押すとサーバーコンソール上に上記のように記述されます。
回答1件
あなたの回答
tips
プレビュー