[Multer] 파일명 관리하기 [diskStorage]

설정·2021년 1월 9일
0
post-thumbnail

Multer Example Code

// index.ejs

<form action="upload" method="POST" enctype="multipart/form-data">
  <input type="file" name="img">
  <input type="submit">
</form>


// index.js

// express
const express = require('express');
const app = express();
const port = 5000;

// multer
const multer = require('multer')
const storage = multer.diskStorage({
  destination: (req, file, callback) => {
    callback(null, 'uploads/')
  },
  filename: (req, file, callback) => {
    callback(null, file.originalname)
  }
})

const upload = multer({storage: storage});

// ejs
app.set('views', __dirname+'/views');
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);

// router
app.get('/', (req, res) => {
  res.render('index')
});

app.post('/upload', upload.single('img'), (req, res)=>{
  console.log("파일업로드했습니다")
  console.log(req.file);
  res.end()
})

// server
app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

Code Review

upload Image

  • resp.png
  • req.file

0개의 댓글