Page 228 - Nodejs 교과서 개정2판
P. 228
const fs = require('fs');
try {
fs.readdirSync('uploads');
} catch (error) {
console.error('uploads 폴더가 없어 uploads 폴더를 생성합니다.');
fs.mkdirSync('uploads');
}
ࢸ աݶ upload ߸ࣻо ࢤӝחؘ ৈӝী নೠ ઙܨ ٜਝযо ٜয णפ
ݢ ੌਸ ೞա݅ স۽٘ೞח ҃ NVMUJQBSU IUNMҗ э ҃ ীח TJOHMF ٜਝযܳ ࢎਊפ
app.post('/upload', upload.single('image'), (req, res) => {
console.log(req.file, req.body);
res.send('ok');
});
single ٜਝযܳ ۄఠ ٜਝয খী ֍যفݶ multer ࢸী ٮۄ ੌ স۽٘ റ req.file ёо ࢤࢿ
ؾפ ੋࣻח input కӒ nameա ಬ ؘఠ ః৬ ੌೞѱ ֍ਵݶ ؾפ স۽٘ ࢿҕ द Ѿҗח req.f
ile ё উী ٜয णפ req.bodyীח ੌ ইצ ؘఠੋ title ٜয णפ
req.file ёח җ э ࢤ҂णפ
{
fieldname: 'img',
originalname: 'nodejs.png',
encoding: '7bit',
mimetype: 'image/png',
destination: 'uploads/',
filename: 'nodejs1514197844339.png',
path: 'uploads\\nodejs1514197844339.png',
size: 53357
}
ৈ۞ ੌਸ স۽٘ೞח ҃ )5.- input కӒীח multipleਸ ॳݶ ؾפ
multipart.html
<form id="form" action="/upload" method="post" enctype="multipart/form-data">