-
<Node.js> multer middleware액티브 스킬/노드 2021. 12. 2. 08:12728x90
🐣
미들웨어란?
라우터와 컨트롤러 사이에 필요한 처리를 한다.
multer란?
파일을 업로드하게 해준다.
왜 사용하는지?
파일을 업로드하기 위해서 사용한다.
어떻게 사용하는지?
https://www.npmjs.com/package/multer
1. 설치
$ npm install --save multer
2. HTML form 요소 설정 (enctype 설정 중요)
<form action="/profile" method="post" enctype="multipart/form-data"> <input type="file" name="avatar" /> </form>
3. 컨트롤러 파일 작성
const express = require('express'); const multer = require('multer'); const upload = multer({ dest: 'uploads/' }); const app = express(); app.post('/profile', upload.single('avatar'), function (req, res, next) { // req.file is the `avatar` file // req.body will hold the text fields, if there were any });
* 업로드한 파일을 읽으려면 Node.js fs 모듈의 fs.readFile 함수 활용
https://nodejs.org/docs/latest-v9.x/api/fs.html#fs_fs_readfile_path_options_callback
728x90'액티브 스킬 > 노드' 카테고리의 다른 글
<Node.js> __dirname 이해하고 가기 (0) 2021.12.19 <Node.js> TypeError: expressHandlebars is not a function (0) 2021.12.11 <Node.js> Express (Routes, Controllers, URL parameter) (0) 2021.11.07 <Node.js> npm run dev 실행 속도 문제 (0) 2021.11.04 <Node.js> App 제작 (파일을 이용해 본문 구현) - 생활코딩 (0) 2021.08.23