วิธีการทำระบบไฟล์อัพโหลดด้วยการใช้ Node.js + Express และ Multer ซึ่งบทความนี้เป็นบทความอัพเดท จากที่เคยเขียนไว้ก่อนหน้านี้ 7-8 แล้วครับ ทำระบบอัพโหลดไฟล์ด้วย Node.js ซึ่งตัว Library มันก็มีการอัพเดทไปพอสมควร
Step 1 - Create Project
เริ่มต้นด้วยการสร้างโปรเจ็คขึ้นมาโดยใช้ npm init หรือ yarn init ก็ได้
yarn init -yจากนั้นก็ทำการติดตั้ง express และ multer
yarn add express multerStep 2 - Frontend (index.html)
ต่อมาสร้างไฟล์ index.html ขึ้นมา โดยส่วนนี้จะเป็นหน้าเว็บธรรมดา ที่มีแค่การอัพโหลดไฟล์ โดยใช้ <input type="file" />
<!doctype html><html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Hello Multer</title> </head> <body> <h1>Hello Multer</h1> <form action="/upload" method="post" enctype="multipart/form-data"> <input type="file" name="photo" /> <button type="submit" value="Upload" /> </form> </body></html> Hello Multer